Plotting Line graph from a Series of 3 column: Matplotlib











up vote
1
down vote

favorite












I have a df which i have manipulated as below



genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']

df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]

print(df_genre_sum)


It gives me result something like :



img



Now, How can I draw a graph where I can see line plot with all genres in different colors with



X axis = Years



Y axis = Revenue



I am new to matplotlib, I know to draw graph when series has index and value but this series has another level inside it.



Update




{'Action': {1984: 735328023.0,
1985: 152427960.0,
1987: 191185897.0,
1989: 962338821.0,
1990: 381293845.0,
1992: 14358033.0,
1993: 212954613.0,
1995: 418194034.0,
1996: 959696788.0,
1997: 967263794.0},
'Adventure': {1984: 361744356.0,
1985: 533537722.0,
1987: 191185897.0,
1989: 1002823860.0,
1990: 244527783.0,
1992: 504050219.0,
1993: 1133054613.0,
1995: 418194034.0,
1996: 678894988.0,
1997: 922401607.0},
'Comedy': {1984: 295212467.0,
1985: 381109762.0,
1987: 38119483.0,
1989: 372485039.0,
1990: 721212258.0,
1992: 518408252.0,
1993: 212954613.0,
1995: 390174654.0,
1996: 353602675.0,
1997: 680494326.0},
'Science Fiction': {1984: 402328023.0,
1985: 381109762.0,
1987: 38119483.0,
1989: 372485039.0,
1990: 244527583.0,
1992: 14358033.0,
1993: 1079155768.0,
1995: 73959291.0,
1996: 293648382.0,
1997: 760510868.0},
'Thriller': {1984: 78371200.0,
1985: 152427960.0,
1987: 191185897.0,
1989: 156167015.0,
1990: 136766262.0,
1992: 658716349.0,
1993: 15668826.0,
1995: 710806752.0,
1996: 978717877.0,
1997: 2178045256.0}}











share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a df which i have manipulated as below



    genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']

    df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]

    print(df_genre_sum)


    It gives me result something like :



    img



    Now, How can I draw a graph where I can see line plot with all genres in different colors with



    X axis = Years



    Y axis = Revenue



    I am new to matplotlib, I know to draw graph when series has index and value but this series has another level inside it.



    Update




    {'Action': {1984: 735328023.0,
    1985: 152427960.0,
    1987: 191185897.0,
    1989: 962338821.0,
    1990: 381293845.0,
    1992: 14358033.0,
    1993: 212954613.0,
    1995: 418194034.0,
    1996: 959696788.0,
    1997: 967263794.0},
    'Adventure': {1984: 361744356.0,
    1985: 533537722.0,
    1987: 191185897.0,
    1989: 1002823860.0,
    1990: 244527783.0,
    1992: 504050219.0,
    1993: 1133054613.0,
    1995: 418194034.0,
    1996: 678894988.0,
    1997: 922401607.0},
    'Comedy': {1984: 295212467.0,
    1985: 381109762.0,
    1987: 38119483.0,
    1989: 372485039.0,
    1990: 721212258.0,
    1992: 518408252.0,
    1993: 212954613.0,
    1995: 390174654.0,
    1996: 353602675.0,
    1997: 680494326.0},
    'Science Fiction': {1984: 402328023.0,
    1985: 381109762.0,
    1987: 38119483.0,
    1989: 372485039.0,
    1990: 244527583.0,
    1992: 14358033.0,
    1993: 1079155768.0,
    1995: 73959291.0,
    1996: 293648382.0,
    1997: 760510868.0},
    'Thriller': {1984: 78371200.0,
    1985: 152427960.0,
    1987: 191185897.0,
    1989: 156167015.0,
    1990: 136766262.0,
    1992: 658716349.0,
    1993: 15668826.0,
    1995: 710806752.0,
    1996: 978717877.0,
    1997: 2178045256.0}}











    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a df which i have manipulated as below



      genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']

      df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]

      print(df_genre_sum)


      It gives me result something like :



      img



      Now, How can I draw a graph where I can see line plot with all genres in different colors with



      X axis = Years



      Y axis = Revenue



      I am new to matplotlib, I know to draw graph when series has index and value but this series has another level inside it.



      Update




      {'Action': {1984: 735328023.0,
      1985: 152427960.0,
      1987: 191185897.0,
      1989: 962338821.0,
      1990: 381293845.0,
      1992: 14358033.0,
      1993: 212954613.0,
      1995: 418194034.0,
      1996: 959696788.0,
      1997: 967263794.0},
      'Adventure': {1984: 361744356.0,
      1985: 533537722.0,
      1987: 191185897.0,
      1989: 1002823860.0,
      1990: 244527783.0,
      1992: 504050219.0,
      1993: 1133054613.0,
      1995: 418194034.0,
      1996: 678894988.0,
      1997: 922401607.0},
      'Comedy': {1984: 295212467.0,
      1985: 381109762.0,
      1987: 38119483.0,
      1989: 372485039.0,
      1990: 721212258.0,
      1992: 518408252.0,
      1993: 212954613.0,
      1995: 390174654.0,
      1996: 353602675.0,
      1997: 680494326.0},
      'Science Fiction': {1984: 402328023.0,
      1985: 381109762.0,
      1987: 38119483.0,
      1989: 372485039.0,
      1990: 244527583.0,
      1992: 14358033.0,
      1993: 1079155768.0,
      1995: 73959291.0,
      1996: 293648382.0,
      1997: 760510868.0},
      'Thriller': {1984: 78371200.0,
      1985: 152427960.0,
      1987: 191185897.0,
      1989: 156167015.0,
      1990: 136766262.0,
      1992: 658716349.0,
      1993: 15668826.0,
      1995: 710806752.0,
      1996: 978717877.0,
      1997: 2178045256.0}}











      share|improve this question















      I have a df which i have manipulated as below



      genres = ['Adventure','Action','Thriller','Science Fiction','Comedy']

      df_genre_sum = df_genre.groupby(['genre','release_year'])['revenue'].sum()[genres]

      print(df_genre_sum)


      It gives me result something like :



      img



      Now, How can I draw a graph where I can see line plot with all genres in different colors with



      X axis = Years



      Y axis = Revenue



      I am new to matplotlib, I know to draw graph when series has index and value but this series has another level inside it.



      Update




      {'Action': {1984: 735328023.0,
      1985: 152427960.0,
      1987: 191185897.0,
      1989: 962338821.0,
      1990: 381293845.0,
      1992: 14358033.0,
      1993: 212954613.0,
      1995: 418194034.0,
      1996: 959696788.0,
      1997: 967263794.0},
      'Adventure': {1984: 361744356.0,
      1985: 533537722.0,
      1987: 191185897.0,
      1989: 1002823860.0,
      1990: 244527783.0,
      1992: 504050219.0,
      1993: 1133054613.0,
      1995: 418194034.0,
      1996: 678894988.0,
      1997: 922401607.0},
      'Comedy': {1984: 295212467.0,
      1985: 381109762.0,
      1987: 38119483.0,
      1989: 372485039.0,
      1990: 721212258.0,
      1992: 518408252.0,
      1993: 212954613.0,
      1995: 390174654.0,
      1996: 353602675.0,
      1997: 680494326.0},
      'Science Fiction': {1984: 402328023.0,
      1985: 381109762.0,
      1987: 38119483.0,
      1989: 372485039.0,
      1990: 244527583.0,
      1992: 14358033.0,
      1993: 1079155768.0,
      1995: 73959291.0,
      1996: 293648382.0,
      1997: 760510868.0},
      'Thriller': {1984: 78371200.0,
      1985: 152427960.0,
      1987: 191185897.0,
      1989: 156167015.0,
      1990: 136766262.0,
      1992: 658716349.0,
      1993: 15668826.0,
      1995: 710806752.0,
      1996: 978717877.0,
      1997: 2178045256.0}}








      pandas dataframe matplotlib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 11:27

























      asked Nov 10 at 9:34









      Shashank Vivek

      4,08732354




      4,08732354
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I believe you need filter first by isin with boolean indexing, then aggregate sum and final Series reshape by unstack:



          df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
          .groupby(['release_year', 'genre'])['revenue']
          .sum()
          .unstack())


          df_genre_sum.plot()





          share|improve this answer























          • The result is empty
            – Shashank Vivek
            Nov 10 at 9:38










          • sorry, edited filtering.
            – jezrael
            Nov 10 at 9:39










          • Can you tell me why the legends to represent color of each genre is missing when I do : plt.plot(df_genre_sum.index,df_genre_sum) plt.title('Change in Revenue of Genre till 2015') plt.xlabel('Years') plt.ylabel('$ in 100 millions'). imgur.com/a/VfUFYKf
            – Shashank Vivek
            Nov 10 at 10:22








          • 1




            Super, you get solution. Also is possible use plt.legend(df_genre_sum.columns)
            – jezrael
            Nov 10 at 11:35






          • 1




            Cool, makes more sense to pass column names. Thanks :)
            – Shashank Vivek
            Nov 11 at 11:28











          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%2f53237654%2fplotting-line-graph-from-a-series-of-3-column-matplotlib%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          I believe you need filter first by isin with boolean indexing, then aggregate sum and final Series reshape by unstack:



          df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
          .groupby(['release_year', 'genre'])['revenue']
          .sum()
          .unstack())


          df_genre_sum.plot()





          share|improve this answer























          • The result is empty
            – Shashank Vivek
            Nov 10 at 9:38










          • sorry, edited filtering.
            – jezrael
            Nov 10 at 9:39










          • Can you tell me why the legends to represent color of each genre is missing when I do : plt.plot(df_genre_sum.index,df_genre_sum) plt.title('Change in Revenue of Genre till 2015') plt.xlabel('Years') plt.ylabel('$ in 100 millions'). imgur.com/a/VfUFYKf
            – Shashank Vivek
            Nov 10 at 10:22








          • 1




            Super, you get solution. Also is possible use plt.legend(df_genre_sum.columns)
            – jezrael
            Nov 10 at 11:35






          • 1




            Cool, makes more sense to pass column names. Thanks :)
            – Shashank Vivek
            Nov 11 at 11:28















          up vote
          1
          down vote



          accepted










          I believe you need filter first by isin with boolean indexing, then aggregate sum and final Series reshape by unstack:



          df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
          .groupby(['release_year', 'genre'])['revenue']
          .sum()
          .unstack())


          df_genre_sum.plot()





          share|improve this answer























          • The result is empty
            – Shashank Vivek
            Nov 10 at 9:38










          • sorry, edited filtering.
            – jezrael
            Nov 10 at 9:39










          • Can you tell me why the legends to represent color of each genre is missing when I do : plt.plot(df_genre_sum.index,df_genre_sum) plt.title('Change in Revenue of Genre till 2015') plt.xlabel('Years') plt.ylabel('$ in 100 millions'). imgur.com/a/VfUFYKf
            – Shashank Vivek
            Nov 10 at 10:22








          • 1




            Super, you get solution. Also is possible use plt.legend(df_genre_sum.columns)
            – jezrael
            Nov 10 at 11:35






          • 1




            Cool, makes more sense to pass column names. Thanks :)
            – Shashank Vivek
            Nov 11 at 11:28













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I believe you need filter first by isin with boolean indexing, then aggregate sum and final Series reshape by unstack:



          df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
          .groupby(['release_year', 'genre'])['revenue']
          .sum()
          .unstack())


          df_genre_sum.plot()





          share|improve this answer














          I believe you need filter first by isin with boolean indexing, then aggregate sum and final Series reshape by unstack:



          df_genre_sum = (df_genre[df_genre['genre'].isin(genres)]
          .groupby(['release_year', 'genre'])['revenue']
          .sum()
          .unstack())


          df_genre_sum.plot()






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 9:41

























          answered Nov 10 at 9:36









          jezrael

          317k22257335




          317k22257335












          • The result is empty
            – Shashank Vivek
            Nov 10 at 9:38










          • sorry, edited filtering.
            – jezrael
            Nov 10 at 9:39










          • Can you tell me why the legends to represent color of each genre is missing when I do : plt.plot(df_genre_sum.index,df_genre_sum) plt.title('Change in Revenue of Genre till 2015') plt.xlabel('Years') plt.ylabel('$ in 100 millions'). imgur.com/a/VfUFYKf
            – Shashank Vivek
            Nov 10 at 10:22








          • 1




            Super, you get solution. Also is possible use plt.legend(df_genre_sum.columns)
            – jezrael
            Nov 10 at 11:35






          • 1




            Cool, makes more sense to pass column names. Thanks :)
            – Shashank Vivek
            Nov 11 at 11:28


















          • The result is empty
            – Shashank Vivek
            Nov 10 at 9:38










          • sorry, edited filtering.
            – jezrael
            Nov 10 at 9:39










          • Can you tell me why the legends to represent color of each genre is missing when I do : plt.plot(df_genre_sum.index,df_genre_sum) plt.title('Change in Revenue of Genre till 2015') plt.xlabel('Years') plt.ylabel('$ in 100 millions'). imgur.com/a/VfUFYKf
            – Shashank Vivek
            Nov 10 at 10:22








          • 1




            Super, you get solution. Also is possible use plt.legend(df_genre_sum.columns)
            – jezrael
            Nov 10 at 11:35






          • 1




            Cool, makes more sense to pass column names. Thanks :)
            – Shashank Vivek
            Nov 11 at 11:28
















          The result is empty
          – Shashank Vivek
          Nov 10 at 9:38




          The result is empty
          – Shashank Vivek
          Nov 10 at 9:38












          sorry, edited filtering.
          – jezrael
          Nov 10 at 9:39




          sorry, edited filtering.
          – jezrael
          Nov 10 at 9:39












          Can you tell me why the legends to represent color of each genre is missing when I do : plt.plot(df_genre_sum.index,df_genre_sum) plt.title('Change in Revenue of Genre till 2015') plt.xlabel('Years') plt.ylabel('$ in 100 millions'). imgur.com/a/VfUFYKf
          – Shashank Vivek
          Nov 10 at 10:22






          Can you tell me why the legends to represent color of each genre is missing when I do : plt.plot(df_genre_sum.index,df_genre_sum) plt.title('Change in Revenue of Genre till 2015') plt.xlabel('Years') plt.ylabel('$ in 100 millions'). imgur.com/a/VfUFYKf
          – Shashank Vivek
          Nov 10 at 10:22






          1




          1




          Super, you get solution. Also is possible use plt.legend(df_genre_sum.columns)
          – jezrael
          Nov 10 at 11:35




          Super, you get solution. Also is possible use plt.legend(df_genre_sum.columns)
          – jezrael
          Nov 10 at 11:35




          1




          1




          Cool, makes more sense to pass column names. Thanks :)
          – Shashank Vivek
          Nov 11 at 11:28




          Cool, makes more sense to pass column names. Thanks :)
          – Shashank Vivek
          Nov 11 at 11:28


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53237654%2fplotting-line-graph-from-a-series-of-3-column-matplotlib%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()