Python matplotlib - How to draw line chart with many series?











up vote
-1
down vote

favorite
1












I had a python pandas dataframe like below:



    test_score  param   # of Nodes
0 0.497852 relu 1
1 0.68935 relu 2
2 0.701165 relu 3
3 0.735223 identity 1
4 0.735064 identity 2
5 0.735691 identity 3
6 0.72651 logistic 1
7 0.664837 logistic 2
8 0.743445 logistic 3
9 0.705182 tanh 1
10 0.673399 tanh 2
11 0.684129 tanh 3


I would like to draw the line chart with x axis as "# of Nodes", y axis as "test_score", and four param values "relu, identity, logistic, tanh" as 4 series lines.



Could this chart be plotted in python matplotlib?










share|improve this question




















  • 1




    Did you try anything? Like df.groupby("param").plot(x="# of nodes", y="test_score")?
    – ImportanceOfBeingErnest
    Nov 11 at 18:16















up vote
-1
down vote

favorite
1












I had a python pandas dataframe like below:



    test_score  param   # of Nodes
0 0.497852 relu 1
1 0.68935 relu 2
2 0.701165 relu 3
3 0.735223 identity 1
4 0.735064 identity 2
5 0.735691 identity 3
6 0.72651 logistic 1
7 0.664837 logistic 2
8 0.743445 logistic 3
9 0.705182 tanh 1
10 0.673399 tanh 2
11 0.684129 tanh 3


I would like to draw the line chart with x axis as "# of Nodes", y axis as "test_score", and four param values "relu, identity, logistic, tanh" as 4 series lines.



Could this chart be plotted in python matplotlib?










share|improve this question




















  • 1




    Did you try anything? Like df.groupby("param").plot(x="# of nodes", y="test_score")?
    – ImportanceOfBeingErnest
    Nov 11 at 18:16













up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





I had a python pandas dataframe like below:



    test_score  param   # of Nodes
0 0.497852 relu 1
1 0.68935 relu 2
2 0.701165 relu 3
3 0.735223 identity 1
4 0.735064 identity 2
5 0.735691 identity 3
6 0.72651 logistic 1
7 0.664837 logistic 2
8 0.743445 logistic 3
9 0.705182 tanh 1
10 0.673399 tanh 2
11 0.684129 tanh 3


I would like to draw the line chart with x axis as "# of Nodes", y axis as "test_score", and four param values "relu, identity, logistic, tanh" as 4 series lines.



Could this chart be plotted in python matplotlib?










share|improve this question















I had a python pandas dataframe like below:



    test_score  param   # of Nodes
0 0.497852 relu 1
1 0.68935 relu 2
2 0.701165 relu 3
3 0.735223 identity 1
4 0.735064 identity 2
5 0.735691 identity 3
6 0.72651 logistic 1
7 0.664837 logistic 2
8 0.743445 logistic 3
9 0.705182 tanh 1
10 0.673399 tanh 2
11 0.684129 tanh 3


I would like to draw the line chart with x axis as "# of Nodes", y axis as "test_score", and four param values "relu, identity, logistic, tanh" as 4 series lines.



Could this chart be plotted in python matplotlib?







python pandas dataframe matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:28

























asked Nov 11 at 18:11









Jenny Jing Yu

123




123








  • 1




    Did you try anything? Like df.groupby("param").plot(x="# of nodes", y="test_score")?
    – ImportanceOfBeingErnest
    Nov 11 at 18:16














  • 1




    Did you try anything? Like df.groupby("param").plot(x="# of nodes", y="test_score")?
    – ImportanceOfBeingErnest
    Nov 11 at 18:16








1




1




Did you try anything? Like df.groupby("param").plot(x="# of nodes", y="test_score")?
– ImportanceOfBeingErnest
Nov 11 at 18:16




Did you try anything? Like df.groupby("param").plot(x="# of nodes", y="test_score")?
– ImportanceOfBeingErnest
Nov 11 at 18:16












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You can start by grouping by param, then iterating through your groups and plotting:



g = df.groupby('param')

for p, data in g:
plt.plot(data['# of Nodes'], data['test_score'], label=p)

plt.legend()
plt.xlabel('# of Nodes')
plt.ylabel('Test Score')

plt.show()


enter image description here






share|improve this answer




























    up vote
    1
    down vote













    If you have all nodes for all parameters like in your example you can .pivot your DataFrame to a more suitable format for plotting them all.



    df.pivot(index='# of Nodes', columns='param', values='test_score').plot()


    enter image description here






    share|improve this answer























      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',
      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%2f53251711%2fpython-matplotlib-how-to-draw-line-chart-with-many-series%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      You can start by grouping by param, then iterating through your groups and plotting:



      g = df.groupby('param')

      for p, data in g:
      plt.plot(data['# of Nodes'], data['test_score'], label=p)

      plt.legend()
      plt.xlabel('# of Nodes')
      plt.ylabel('Test Score')

      plt.show()


      enter image description here






      share|improve this answer

























        up vote
        0
        down vote



        accepted










        You can start by grouping by param, then iterating through your groups and plotting:



        g = df.groupby('param')

        for p, data in g:
        plt.plot(data['# of Nodes'], data['test_score'], label=p)

        plt.legend()
        plt.xlabel('# of Nodes')
        plt.ylabel('Test Score')

        plt.show()


        enter image description here






        share|improve this answer























          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can start by grouping by param, then iterating through your groups and plotting:



          g = df.groupby('param')

          for p, data in g:
          plt.plot(data['# of Nodes'], data['test_score'], label=p)

          plt.legend()
          plt.xlabel('# of Nodes')
          plt.ylabel('Test Score')

          plt.show()


          enter image description here






          share|improve this answer












          You can start by grouping by param, then iterating through your groups and plotting:



          g = df.groupby('param')

          for p, data in g:
          plt.plot(data['# of Nodes'], data['test_score'], label=p)

          plt.legend()
          plt.xlabel('# of Nodes')
          plt.ylabel('Test Score')

          plt.show()


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 18:17









          sacul

          29k41639




          29k41639
























              up vote
              1
              down vote













              If you have all nodes for all parameters like in your example you can .pivot your DataFrame to a more suitable format for plotting them all.



              df.pivot(index='# of Nodes', columns='param', values='test_score').plot()


              enter image description here






              share|improve this answer



























                up vote
                1
                down vote













                If you have all nodes for all parameters like in your example you can .pivot your DataFrame to a more suitable format for plotting them all.



                df.pivot(index='# of Nodes', columns='param', values='test_score').plot()


                enter image description here






                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  If you have all nodes for all parameters like in your example you can .pivot your DataFrame to a more suitable format for plotting them all.



                  df.pivot(index='# of Nodes', columns='param', values='test_score').plot()


                  enter image description here






                  share|improve this answer














                  If you have all nodes for all parameters like in your example you can .pivot your DataFrame to a more suitable format for plotting them all.



                  df.pivot(index='# of Nodes', columns='param', values='test_score').plot()


                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 21:43

























                  answered Nov 11 at 21:33









                  ALollz

                  10.6k31234




                  10.6k31234






























                      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%2f53251711%2fpython-matplotlib-how-to-draw-line-chart-with-many-series%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







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python