Difference between ax.set_xlabel() and ax.xaxis.set_label() in MatplotLib 3.0.1










5















(This may be a naive question. Pardon me if so.)



I am using Python 3.7.1 and MatplotLib 3.0.1.



In below simple code, I used different ways to assign labels to axes. They seem to be equivalent from an OO perspective. But only the x axis label shows. Why?



import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.linspace(-2,2,60)
y = np.linspace(-2,2,60)

xx, yy = np.meshgrid(x,y)

z = xx**2 + yy**3

surf = ax.plot_surface(xx, yy, z, cmap="viridis")

ax.set_xlabel('x axis') # <====== This works
ax.yaxis.set_label('y axis') # <====== This doesn't work
ax.zaxis.set_label('z axis') # <====== This doesn't work


In below image, you can see only the label x axis is shown.



enter image description here



ADD 1



According to here, it seems the matplotlib.axis.XAxis.set_label() approach should work.



In my case, the ax.xaxis is of type mpl_toolkits.mplot3d.axis3d.XAxis. It's a little different type. But I think it should work as well.










share|improve this question




























    5















    (This may be a naive question. Pardon me if so.)



    I am using Python 3.7.1 and MatplotLib 3.0.1.



    In below simple code, I used different ways to assign labels to axes. They seem to be equivalent from an OO perspective. But only the x axis label shows. Why?



    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    x = np.linspace(-2,2,60)
    y = np.linspace(-2,2,60)

    xx, yy = np.meshgrid(x,y)

    z = xx**2 + yy**3

    surf = ax.plot_surface(xx, yy, z, cmap="viridis")

    ax.set_xlabel('x axis') # <====== This works
    ax.yaxis.set_label('y axis') # <====== This doesn't work
    ax.zaxis.set_label('z axis') # <====== This doesn't work


    In below image, you can see only the label x axis is shown.



    enter image description here



    ADD 1



    According to here, it seems the matplotlib.axis.XAxis.set_label() approach should work.



    In my case, the ax.xaxis is of type mpl_toolkits.mplot3d.axis3d.XAxis. It's a little different type. But I think it should work as well.










    share|improve this question


























      5












      5








      5


      1






      (This may be a naive question. Pardon me if so.)



      I am using Python 3.7.1 and MatplotLib 3.0.1.



      In below simple code, I used different ways to assign labels to axes. They seem to be equivalent from an OO perspective. But only the x axis label shows. Why?



      import matplotlib.pyplot as plt
      import numpy as np
      from mpl_toolkits.mplot3d import Axes3D

      fig = plt.figure()
      ax = fig.add_subplot(111, projection='3d')

      x = np.linspace(-2,2,60)
      y = np.linspace(-2,2,60)

      xx, yy = np.meshgrid(x,y)

      z = xx**2 + yy**3

      surf = ax.plot_surface(xx, yy, z, cmap="viridis")

      ax.set_xlabel('x axis') # <====== This works
      ax.yaxis.set_label('y axis') # <====== This doesn't work
      ax.zaxis.set_label('z axis') # <====== This doesn't work


      In below image, you can see only the label x axis is shown.



      enter image description here



      ADD 1



      According to here, it seems the matplotlib.axis.XAxis.set_label() approach should work.



      In my case, the ax.xaxis is of type mpl_toolkits.mplot3d.axis3d.XAxis. It's a little different type. But I think it should work as well.










      share|improve this question
















      (This may be a naive question. Pardon me if so.)



      I am using Python 3.7.1 and MatplotLib 3.0.1.



      In below simple code, I used different ways to assign labels to axes. They seem to be equivalent from an OO perspective. But only the x axis label shows. Why?



      import matplotlib.pyplot as plt
      import numpy as np
      from mpl_toolkits.mplot3d import Axes3D

      fig = plt.figure()
      ax = fig.add_subplot(111, projection='3d')

      x = np.linspace(-2,2,60)
      y = np.linspace(-2,2,60)

      xx, yy = np.meshgrid(x,y)

      z = xx**2 + yy**3

      surf = ax.plot_surface(xx, yy, z, cmap="viridis")

      ax.set_xlabel('x axis') # <====== This works
      ax.yaxis.set_label('y axis') # <====== This doesn't work
      ax.zaxis.set_label('z axis') # <====== This doesn't work


      In below image, you can see only the label x axis is shown.



      enter image description here



      ADD 1



      According to here, it seems the matplotlib.axis.XAxis.set_label() approach should work.



      In my case, the ax.xaxis is of type mpl_toolkits.mplot3d.axis3d.XAxis. It's a little different type. But I think it should work as well.







      python python-3.x matplotlib mplot3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 3:25









      DavidG

      11.5k103544




      11.5k103544










      asked Nov 15 '18 at 2:44









      smwikipediasmwikipedia

      21.9k64216372




      21.9k64216372






















          2 Answers
          2






          active

          oldest

          votes


















          3














          There are several things called "labels" in matplotlib. There are e.g. axis labels, set via set_xlabel, there are tick labels, set via set_ticklabels, and there are artist labels, set via set_label.



          Every artist in matplotlib has a label attribute. You usually encounter it when setting a label for it to be shown in the legend,



          plt.plot(..., label="my label")


          but it may be helpful for other cases as well. The above is equivalent to



          line, = plt.plot(...)
          line.set_label("mylabel")


          So not only the Line2D object created by plot has a label, but equally the matplotlib.axis.XAxis has a label. This is the one you set with ax.yaxis.set_label. However, this is not by default taken into account when producing a legend, so its usefulness is questionable for the end user.



          For the labels of the axes, set_ylabel is really the method to use for setting the ylabel. Internally, this would call yaxis.set_label_text, so as correctly pointed out by @DavidG, you can replace ax.set_ylabel(..) by ax.yaxis.set_label_text(..) (except for an additional argument labelpad that is taken by set_ylabel, but not by set_label_text.






          share|improve this answer






























            3














            Looking at the documentation you have shown for matplotlib.axis.XAxis.set_label() it appears this is to do with assigning a label to the axis itself for potential use in a legend (see @ImportanceOfBeingErnest's answer for more explanation)



            In any case, the equivalent to ax.set_xlabel is matplotlib.axis.Axis.set_label_text



            Therefore changing the code to:



            ax.set_xlabel('x axis') 
            ax.yaxis.set_label_text('y axis')
            ax.zaxis.set_label_text('z axis')


            Gives the desired output:



            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',
              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%2f53311685%2fdifference-between-ax-set-xlabel-and-ax-xaxis-set-label-in-matplotlib-3-0-1%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









              3














              There are several things called "labels" in matplotlib. There are e.g. axis labels, set via set_xlabel, there are tick labels, set via set_ticklabels, and there are artist labels, set via set_label.



              Every artist in matplotlib has a label attribute. You usually encounter it when setting a label for it to be shown in the legend,



              plt.plot(..., label="my label")


              but it may be helpful for other cases as well. The above is equivalent to



              line, = plt.plot(...)
              line.set_label("mylabel")


              So not only the Line2D object created by plot has a label, but equally the matplotlib.axis.XAxis has a label. This is the one you set with ax.yaxis.set_label. However, this is not by default taken into account when producing a legend, so its usefulness is questionable for the end user.



              For the labels of the axes, set_ylabel is really the method to use for setting the ylabel. Internally, this would call yaxis.set_label_text, so as correctly pointed out by @DavidG, you can replace ax.set_ylabel(..) by ax.yaxis.set_label_text(..) (except for an additional argument labelpad that is taken by set_ylabel, but not by set_label_text.






              share|improve this answer



























                3














                There are several things called "labels" in matplotlib. There are e.g. axis labels, set via set_xlabel, there are tick labels, set via set_ticklabels, and there are artist labels, set via set_label.



                Every artist in matplotlib has a label attribute. You usually encounter it when setting a label for it to be shown in the legend,



                plt.plot(..., label="my label")


                but it may be helpful for other cases as well. The above is equivalent to



                line, = plt.plot(...)
                line.set_label("mylabel")


                So not only the Line2D object created by plot has a label, but equally the matplotlib.axis.XAxis has a label. This is the one you set with ax.yaxis.set_label. However, this is not by default taken into account when producing a legend, so its usefulness is questionable for the end user.



                For the labels of the axes, set_ylabel is really the method to use for setting the ylabel. Internally, this would call yaxis.set_label_text, so as correctly pointed out by @DavidG, you can replace ax.set_ylabel(..) by ax.yaxis.set_label_text(..) (except for an additional argument labelpad that is taken by set_ylabel, but not by set_label_text.






                share|improve this answer

























                  3












                  3








                  3







                  There are several things called "labels" in matplotlib. There are e.g. axis labels, set via set_xlabel, there are tick labels, set via set_ticklabels, and there are artist labels, set via set_label.



                  Every artist in matplotlib has a label attribute. You usually encounter it when setting a label for it to be shown in the legend,



                  plt.plot(..., label="my label")


                  but it may be helpful for other cases as well. The above is equivalent to



                  line, = plt.plot(...)
                  line.set_label("mylabel")


                  So not only the Line2D object created by plot has a label, but equally the matplotlib.axis.XAxis has a label. This is the one you set with ax.yaxis.set_label. However, this is not by default taken into account when producing a legend, so its usefulness is questionable for the end user.



                  For the labels of the axes, set_ylabel is really the method to use for setting the ylabel. Internally, this would call yaxis.set_label_text, so as correctly pointed out by @DavidG, you can replace ax.set_ylabel(..) by ax.yaxis.set_label_text(..) (except for an additional argument labelpad that is taken by set_ylabel, but not by set_label_text.






                  share|improve this answer













                  There are several things called "labels" in matplotlib. There are e.g. axis labels, set via set_xlabel, there are tick labels, set via set_ticklabels, and there are artist labels, set via set_label.



                  Every artist in matplotlib has a label attribute. You usually encounter it when setting a label for it to be shown in the legend,



                  plt.plot(..., label="my label")


                  but it may be helpful for other cases as well. The above is equivalent to



                  line, = plt.plot(...)
                  line.set_label("mylabel")


                  So not only the Line2D object created by plot has a label, but equally the matplotlib.axis.XAxis has a label. This is the one you set with ax.yaxis.set_label. However, this is not by default taken into account when producing a legend, so its usefulness is questionable for the end user.



                  For the labels of the axes, set_ylabel is really the method to use for setting the ylabel. Internally, this would call yaxis.set_label_text, so as correctly pointed out by @DavidG, you can replace ax.set_ylabel(..) by ax.yaxis.set_label_text(..) (except for an additional argument labelpad that is taken by set_ylabel, but not by set_label_text.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 3:48









                  ImportanceOfBeingErnestImportanceOfBeingErnest

                  139k13162240




                  139k13162240























                      3














                      Looking at the documentation you have shown for matplotlib.axis.XAxis.set_label() it appears this is to do with assigning a label to the axis itself for potential use in a legend (see @ImportanceOfBeingErnest's answer for more explanation)



                      In any case, the equivalent to ax.set_xlabel is matplotlib.axis.Axis.set_label_text



                      Therefore changing the code to:



                      ax.set_xlabel('x axis') 
                      ax.yaxis.set_label_text('y axis')
                      ax.zaxis.set_label_text('z axis')


                      Gives the desired output:



                      enter image description here






                      share|improve this answer





























                        3














                        Looking at the documentation you have shown for matplotlib.axis.XAxis.set_label() it appears this is to do with assigning a label to the axis itself for potential use in a legend (see @ImportanceOfBeingErnest's answer for more explanation)



                        In any case, the equivalent to ax.set_xlabel is matplotlib.axis.Axis.set_label_text



                        Therefore changing the code to:



                        ax.set_xlabel('x axis') 
                        ax.yaxis.set_label_text('y axis')
                        ax.zaxis.set_label_text('z axis')


                        Gives the desired output:



                        enter image description here






                        share|improve this answer



























                          3












                          3








                          3







                          Looking at the documentation you have shown for matplotlib.axis.XAxis.set_label() it appears this is to do with assigning a label to the axis itself for potential use in a legend (see @ImportanceOfBeingErnest's answer for more explanation)



                          In any case, the equivalent to ax.set_xlabel is matplotlib.axis.Axis.set_label_text



                          Therefore changing the code to:



                          ax.set_xlabel('x axis') 
                          ax.yaxis.set_label_text('y axis')
                          ax.zaxis.set_label_text('z axis')


                          Gives the desired output:



                          enter image description here






                          share|improve this answer















                          Looking at the documentation you have shown for matplotlib.axis.XAxis.set_label() it appears this is to do with assigning a label to the axis itself for potential use in a legend (see @ImportanceOfBeingErnest's answer for more explanation)



                          In any case, the equivalent to ax.set_xlabel is matplotlib.axis.Axis.set_label_text



                          Therefore changing the code to:



                          ax.set_xlabel('x axis') 
                          ax.yaxis.set_label_text('y axis')
                          ax.zaxis.set_label_text('z axis')


                          Gives the desired output:



                          enter image description here







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 15 '18 at 3:56

























                          answered Nov 15 '18 at 3:21









                          DavidGDavidG

                          11.5k103544




                          11.5k103544



























                              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%2f53311685%2fdifference-between-ax-set-xlabel-and-ax-xaxis-set-label-in-matplotlib-3-0-1%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

                              Darth Vader #20

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Ondo