how to plot histogram in matplotlib when data is in tuples?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I need to plot a histogram of the 5 most frequently occurring words in a list. I've used the collections module's c.counter().most_common() to give me the following tuples:



[('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]


How can I plot a histogram when the data is in the format ('word', frequency)?



The format that I am familiar with is: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]



I know that I could multiply the string times the integer in each element to build a new list in the format I am familiar with to plot on the histogram but I feel like there has to be a more efficient way to do this.










share|improve this question





























    1















    I need to plot a histogram of the 5 most frequently occurring words in a list. I've used the collections module's c.counter().most_common() to give me the following tuples:



    [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]


    How can I plot a histogram when the data is in the format ('word', frequency)?



    The format that I am familiar with is: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]



    I know that I could multiply the string times the integer in each element to build a new list in the format I am familiar with to plot on the histogram but I feel like there has to be a more efficient way to do this.










    share|improve this question

























      1












      1








      1








      I need to plot a histogram of the 5 most frequently occurring words in a list. I've used the collections module's c.counter().most_common() to give me the following tuples:



      [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]


      How can I plot a histogram when the data is in the format ('word', frequency)?



      The format that I am familiar with is: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]



      I know that I could multiply the string times the integer in each element to build a new list in the format I am familiar with to plot on the histogram but I feel like there has to be a more efficient way to do this.










      share|improve this question














      I need to plot a histogram of the 5 most frequently occurring words in a list. I've used the collections module's c.counter().most_common() to give me the following tuples:



      [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]


      How can I plot a histogram when the data is in the format ('word', frequency)?



      The format that I am familiar with is: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]



      I know that I could multiply the string times the integer in each element to build a new list in the format I am familiar with to plot on the histogram but I feel like there has to be a more efficient way to do this.







      python matplotlib






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 7:12









      Jacob MyerJacob Myer

      666




      666
























          4 Answers
          4






          active

          oldest

          votes


















          4














          Unzip your list of tuples:



          from matplotlib import pyplot as plt

          a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

          plt.bar(*zip(*a))
          plt.show()


          Sample output:
          enter image description here






          share|improve this answer

































            1














            You can use matplotlib bar chart:



            import matplotlib.pyplot as plt; plt.rcdefaults()
            import numpy as np
            import matplotlib.pyplot as plt


            items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
            y_pos = np.arange(len(items))
            plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
            plt.xticks(y_pos, [x[0] for x in items])

            plt.show()


            With the result:
            Bar Chart






            share|improve this answer































              1














              I prefer pandas for easy manipulation of data and plotting:



              import pandas

              freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

              # Create a DataFrame for the data, with names for the columns
              freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
              freqdf.plot.barh()


              Resulting plot:



              Bar plot






              share|improve this answer
























              • Thank you but this was for an exercise in matplotlib unfortunately

                – Jacob Myer
                Nov 25 '18 at 7:49



















              1














              Here's an extension of above solution using Matplotlib as well as Seaborn:



              import numpy as np
              import matplotlib.pyplot as plt
              import seaborn as sns

              lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

              val, cnt = (zip(*lst))
              val, cnt = list(val), list(cnt)
              val, cnt
              # (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])

              # using Matplotlib
              length = len(cnt)
              plt.bar(np.arange(length), cnt, label=True)
              plt.xticks(np.arange(len(cnt)), val)
              plt.show()


              matpl



              # using seaborn 
              sns.barplot( val, cnt )


              sea






              share|improve this answer



















              • 1





                Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.

                – Jacob Myer
                Nov 25 '18 at 7:50












              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465421%2fhow-to-plot-histogram-in-matplotlib-when-data-is-in-tuples%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              Unzip your list of tuples:



              from matplotlib import pyplot as plt

              a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

              plt.bar(*zip(*a))
              plt.show()


              Sample output:
              enter image description here






              share|improve this answer






























                4














                Unzip your list of tuples:



                from matplotlib import pyplot as plt

                a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                plt.bar(*zip(*a))
                plt.show()


                Sample output:
                enter image description here






                share|improve this answer




























                  4












                  4








                  4







                  Unzip your list of tuples:



                  from matplotlib import pyplot as plt

                  a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                  plt.bar(*zip(*a))
                  plt.show()


                  Sample output:
                  enter image description here






                  share|improve this answer















                  Unzip your list of tuples:



                  from matplotlib import pyplot as plt

                  a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                  plt.bar(*zip(*a))
                  plt.show()


                  Sample output:
                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 7:37

























                  answered Nov 25 '18 at 7:28









                  Mr. TMr. T

                  4,22391636




                  4,22391636

























                      1














                      You can use matplotlib bar chart:



                      import matplotlib.pyplot as plt; plt.rcdefaults()
                      import numpy as np
                      import matplotlib.pyplot as plt


                      items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
                      y_pos = np.arange(len(items))
                      plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
                      plt.xticks(y_pos, [x[0] for x in items])

                      plt.show()


                      With the result:
                      Bar Chart






                      share|improve this answer




























                        1














                        You can use matplotlib bar chart:



                        import matplotlib.pyplot as plt; plt.rcdefaults()
                        import numpy as np
                        import matplotlib.pyplot as plt


                        items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
                        y_pos = np.arange(len(items))
                        plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
                        plt.xticks(y_pos, [x[0] for x in items])

                        plt.show()


                        With the result:
                        Bar Chart






                        share|improve this answer


























                          1












                          1








                          1







                          You can use matplotlib bar chart:



                          import matplotlib.pyplot as plt; plt.rcdefaults()
                          import numpy as np
                          import matplotlib.pyplot as plt


                          items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
                          y_pos = np.arange(len(items))
                          plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
                          plt.xticks(y_pos, [x[0] for x in items])

                          plt.show()


                          With the result:
                          Bar Chart






                          share|improve this answer













                          You can use matplotlib bar chart:



                          import matplotlib.pyplot as plt; plt.rcdefaults()
                          import numpy as np
                          import matplotlib.pyplot as plt


                          items = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
                          y_pos = np.arange(len(items))
                          plt.bar(y_pos, [x[1] for x in items], align='center', alpha=0.5)
                          plt.xticks(y_pos, [x[0] for x in items])

                          plt.show()


                          With the result:
                          Bar Chart







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 25 '18 at 7:17









                          DinariDinari

                          1,709623




                          1,709623























                              1














                              I prefer pandas for easy manipulation of data and plotting:



                              import pandas

                              freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                              # Create a DataFrame for the data, with names for the columns
                              freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
                              freqdf.plot.barh()


                              Resulting plot:



                              Bar plot






                              share|improve this answer
























                              • Thank you but this was for an exercise in matplotlib unfortunately

                                – Jacob Myer
                                Nov 25 '18 at 7:49
















                              1














                              I prefer pandas for easy manipulation of data and plotting:



                              import pandas

                              freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                              # Create a DataFrame for the data, with names for the columns
                              freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
                              freqdf.plot.barh()


                              Resulting plot:



                              Bar plot






                              share|improve this answer
























                              • Thank you but this was for an exercise in matplotlib unfortunately

                                – Jacob Myer
                                Nov 25 '18 at 7:49














                              1












                              1








                              1







                              I prefer pandas for easy manipulation of data and plotting:



                              import pandas

                              freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                              # Create a DataFrame for the data, with names for the columns
                              freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
                              freqdf.plot.barh()


                              Resulting plot:



                              Bar plot






                              share|improve this answer













                              I prefer pandas for easy manipulation of data and plotting:



                              import pandas

                              freqs = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                              # Create a DataFrame for the data, with names for the columns
                              freqdf = pandas.DataFrame(freqs, columns=['Word', 'Count']).set_index('Word')
                              freqdf.plot.barh()


                              Resulting plot:



                              Bar plot







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 25 '18 at 7:29









                              chthonicdaemonchthonicdaemon

                              12.6k3147




                              12.6k3147













                              • Thank you but this was for an exercise in matplotlib unfortunately

                                – Jacob Myer
                                Nov 25 '18 at 7:49



















                              • Thank you but this was for an exercise in matplotlib unfortunately

                                – Jacob Myer
                                Nov 25 '18 at 7:49

















                              Thank you but this was for an exercise in matplotlib unfortunately

                              – Jacob Myer
                              Nov 25 '18 at 7:49





                              Thank you but this was for an exercise in matplotlib unfortunately

                              – Jacob Myer
                              Nov 25 '18 at 7:49











                              1














                              Here's an extension of above solution using Matplotlib as well as Seaborn:



                              import numpy as np
                              import matplotlib.pyplot as plt
                              import seaborn as sns

                              lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                              val, cnt = (zip(*lst))
                              val, cnt = list(val), list(cnt)
                              val, cnt
                              # (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])

                              # using Matplotlib
                              length = len(cnt)
                              plt.bar(np.arange(length), cnt, label=True)
                              plt.xticks(np.arange(len(cnt)), val)
                              plt.show()


                              matpl



                              # using seaborn 
                              sns.barplot( val, cnt )


                              sea






                              share|improve this answer



















                              • 1





                                Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.

                                – Jacob Myer
                                Nov 25 '18 at 7:50
















                              1














                              Here's an extension of above solution using Matplotlib as well as Seaborn:



                              import numpy as np
                              import matplotlib.pyplot as plt
                              import seaborn as sns

                              lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                              val, cnt = (zip(*lst))
                              val, cnt = list(val), list(cnt)
                              val, cnt
                              # (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])

                              # using Matplotlib
                              length = len(cnt)
                              plt.bar(np.arange(length), cnt, label=True)
                              plt.xticks(np.arange(len(cnt)), val)
                              plt.show()


                              matpl



                              # using seaborn 
                              sns.barplot( val, cnt )


                              sea






                              share|improve this answer



















                              • 1





                                Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.

                                – Jacob Myer
                                Nov 25 '18 at 7:50














                              1












                              1








                              1







                              Here's an extension of above solution using Matplotlib as well as Seaborn:



                              import numpy as np
                              import matplotlib.pyplot as plt
                              import seaborn as sns

                              lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                              val, cnt = (zip(*lst))
                              val, cnt = list(val), list(cnt)
                              val, cnt
                              # (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])

                              # using Matplotlib
                              length = len(cnt)
                              plt.bar(np.arange(length), cnt, label=True)
                              plt.xticks(np.arange(len(cnt)), val)
                              plt.show()


                              matpl



                              # using seaborn 
                              sns.barplot( val, cnt )


                              sea






                              share|improve this answer













                              Here's an extension of above solution using Matplotlib as well as Seaborn:



                              import numpy as np
                              import matplotlib.pyplot as plt
                              import seaborn as sns

                              lst = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

                              val, cnt = (zip(*lst))
                              val, cnt = list(val), list(cnt)
                              val, cnt
                              # (['you', 'i', 'we', 'my', 'he'], [7706, 6570, 2733, 2718, 2369])

                              # using Matplotlib
                              length = len(cnt)
                              plt.bar(np.arange(length), cnt, label=True)
                              plt.xticks(np.arange(len(cnt)), val)
                              plt.show()


                              matpl



                              # using seaborn 
                              sns.barplot( val, cnt )


                              sea







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 25 '18 at 7:37









                              dataLeodataLeo

                              6531520




                              6531520








                              • 1





                                Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.

                                – Jacob Myer
                                Nov 25 '18 at 7:50














                              • 1





                                Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.

                                – Jacob Myer
                                Nov 25 '18 at 7:50








                              1




                              1





                              Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.

                              – Jacob Myer
                              Nov 25 '18 at 7:50





                              Thank you, I would prefer seaborn but I had to use matplotlib for this particular exercise.

                              – Jacob Myer
                              Nov 25 '18 at 7:50


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465421%2fhow-to-plot-histogram-in-matplotlib-when-data-is-in-tuples%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()