Specifying values for my x-axis using the matplotlib.pyplot?











up vote
1
down vote

favorite












I am not able to specify values ​​for my x-axis , using the matplotlib.pyplot.



In some images the chart.xticks(years) solves the problem , but it seems that when the set of x-axis values ​​is too small , it uses default values like ​​[0,1,2,...,N].



A case that works:
Image



A case that does not works:
Image



My code:



import matplotlib.pyplot as chart
from matplotlib import lines

# Settings
chart.title(file_name)
chart.xlabel('Years')
chart.ylabel('Committers/Contributions')
chart.ylim([0,highest_value + 100])
chart.xlim(first_year,2017)

# Values
committer_line = chart.plot(committers_dict.keys(),committers_dict.values(),'r',label='Committer')
contribution_line = chart.plot(contributions_dict.keys(),contributions_dict.values(),'b--',label='Contribution')
years = list(range(first_year,2017))
chart.xticks(years)

# Legend
chart.legend()

# Show/Save
chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
chart.show()









share|improve this question
























  • Note that it is not reverting to [0,1,2...N] as in the bottom right you can see +2.011e3 meaning it is actually plotting 2011,2012 and so on, so it is just a matter of how you are showing it.
    – Alessandro
    Mar 23 '16 at 18:07















up vote
1
down vote

favorite












I am not able to specify values ​​for my x-axis , using the matplotlib.pyplot.



In some images the chart.xticks(years) solves the problem , but it seems that when the set of x-axis values ​​is too small , it uses default values like ​​[0,1,2,...,N].



A case that works:
Image



A case that does not works:
Image



My code:



import matplotlib.pyplot as chart
from matplotlib import lines

# Settings
chart.title(file_name)
chart.xlabel('Years')
chart.ylabel('Committers/Contributions')
chart.ylim([0,highest_value + 100])
chart.xlim(first_year,2017)

# Values
committer_line = chart.plot(committers_dict.keys(),committers_dict.values(),'r',label='Committer')
contribution_line = chart.plot(contributions_dict.keys(),contributions_dict.values(),'b--',label='Contribution')
years = list(range(first_year,2017))
chart.xticks(years)

# Legend
chart.legend()

# Show/Save
chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
chart.show()









share|improve this question
























  • Note that it is not reverting to [0,1,2...N] as in the bottom right you can see +2.011e3 meaning it is actually plotting 2011,2012 and so on, so it is just a matter of how you are showing it.
    – Alessandro
    Mar 23 '16 at 18:07













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am not able to specify values ​​for my x-axis , using the matplotlib.pyplot.



In some images the chart.xticks(years) solves the problem , but it seems that when the set of x-axis values ​​is too small , it uses default values like ​​[0,1,2,...,N].



A case that works:
Image



A case that does not works:
Image



My code:



import matplotlib.pyplot as chart
from matplotlib import lines

# Settings
chart.title(file_name)
chart.xlabel('Years')
chart.ylabel('Committers/Contributions')
chart.ylim([0,highest_value + 100])
chart.xlim(first_year,2017)

# Values
committer_line = chart.plot(committers_dict.keys(),committers_dict.values(),'r',label='Committer')
contribution_line = chart.plot(contributions_dict.keys(),contributions_dict.values(),'b--',label='Contribution')
years = list(range(first_year,2017))
chart.xticks(years)

# Legend
chart.legend()

# Show/Save
chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
chart.show()









share|improve this question















I am not able to specify values ​​for my x-axis , using the matplotlib.pyplot.



In some images the chart.xticks(years) solves the problem , but it seems that when the set of x-axis values ​​is too small , it uses default values like ​​[0,1,2,...,N].



A case that works:
Image



A case that does not works:
Image



My code:



import matplotlib.pyplot as chart
from matplotlib import lines

# Settings
chart.title(file_name)
chart.xlabel('Years')
chart.ylabel('Committers/Contributions')
chart.ylim([0,highest_value + 100])
chart.xlim(first_year,2017)

# Values
committer_line = chart.plot(committers_dict.keys(),committers_dict.values(),'r',label='Committer')
contribution_line = chart.plot(contributions_dict.keys(),contributions_dict.values(),'b--',label='Contribution')
years = list(range(first_year,2017))
chart.xticks(years)

# Legend
chart.legend()

# Show/Save
chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
chart.show()






python matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 7:03









Cœur

17k9102140




17k9102140










asked Mar 23 '16 at 17:32









Felipe Fronchetti

225




225












  • Note that it is not reverting to [0,1,2...N] as in the bottom right you can see +2.011e3 meaning it is actually plotting 2011,2012 and so on, so it is just a matter of how you are showing it.
    – Alessandro
    Mar 23 '16 at 18:07


















  • Note that it is not reverting to [0,1,2...N] as in the bottom right you can see +2.011e3 meaning it is actually plotting 2011,2012 and so on, so it is just a matter of how you are showing it.
    – Alessandro
    Mar 23 '16 at 18:07
















Note that it is not reverting to [0,1,2...N] as in the bottom right you can see +2.011e3 meaning it is actually plotting 2011,2012 and so on, so it is just a matter of how you are showing it.
– Alessandro
Mar 23 '16 at 18:07




Note that it is not reverting to [0,1,2...N] as in the bottom right you can see +2.011e3 meaning it is actually plotting 2011,2012 and so on, so it is just a matter of how you are showing it.
– Alessandro
Mar 23 '16 at 18:07












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Matplotlib is putting the correct numbers but in scientific notation because there's no more room in the x-axis to put more stuff. That said you can interact directly with the x-axis and state what strings do you want, in what positions, using a value for rotation. The following example (data was randomized):



import matplotlib.pyplot as chart
from matplotlib import lines
import random

title = 'Title'
first_year = 1960
last_year = 2017
x = [year for year in range(first_year,2017)]
y1 = [random.randint(0,10) for year in range(first_year,2017)]
y2 = [random.randint(0,10) for year in range(first_year,2017)]
highest_value = max(y1+y2)

# Settings
chart.title(title)
chart.xlabel('Years')
chart.ylabel('Committers/Contributions')
chart.ylim([0,highest_value + 0.05*highest_value])
chart.xlim(first_year,last_year)

# Values
committer_line = chart.plot(x,y1,'r',label='Committer')
contribution_line = chart.plot(x,y2,'b--',label='Contribution')
years = list(range(first_year,last_year))
years_str = [str(i) for i in range(first_year,last_year)]
chart.xticks(years,years_str,rotation=45)

# Legend
chart.legend()

# Show/Save
#chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
chart.show()


, results in:



lots of dates in x axis



, which is a bit cluttered so giving a step to you "Label Code" range:



years = list(range(first_year,last_year,5)) # It's 5!
years_str = [str(i) for i in range(first_year,last_year,5)] # It's 5!


, will ease the x axis information:



a few dates on the x axis






share|improve this answer




























    up vote
    1
    down vote













    If you look in the lower right hand corner, you'll see that matplotlib simply changed the display of the xtick labels to use scientific notation to save space. To change this you will want to alter the tick label Formatter.



    To disable this conversion to scientific notation, you can tweak the default formatter:



    ax.get_xaxis().get_major_formatter().set_useOffset(False)


    This edits the useOffset parameter of your ScalarFormatter.



    If you want to disable this behavior by default, you can edit the axes.formatter.useoffset rcparam.






    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%2f36184953%2fspecifying-values-for-my-x-axis-using-the-matplotlib-pyplot%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
      1
      down vote



      accepted










      Matplotlib is putting the correct numbers but in scientific notation because there's no more room in the x-axis to put more stuff. That said you can interact directly with the x-axis and state what strings do you want, in what positions, using a value for rotation. The following example (data was randomized):



      import matplotlib.pyplot as chart
      from matplotlib import lines
      import random

      title = 'Title'
      first_year = 1960
      last_year = 2017
      x = [year for year in range(first_year,2017)]
      y1 = [random.randint(0,10) for year in range(first_year,2017)]
      y2 = [random.randint(0,10) for year in range(first_year,2017)]
      highest_value = max(y1+y2)

      # Settings
      chart.title(title)
      chart.xlabel('Years')
      chart.ylabel('Committers/Contributions')
      chart.ylim([0,highest_value + 0.05*highest_value])
      chart.xlim(first_year,last_year)

      # Values
      committer_line = chart.plot(x,y1,'r',label='Committer')
      contribution_line = chart.plot(x,y2,'b--',label='Contribution')
      years = list(range(first_year,last_year))
      years_str = [str(i) for i in range(first_year,last_year)]
      chart.xticks(years,years_str,rotation=45)

      # Legend
      chart.legend()

      # Show/Save
      #chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
      chart.show()


      , results in:



      lots of dates in x axis



      , which is a bit cluttered so giving a step to you "Label Code" range:



      years = list(range(first_year,last_year,5)) # It's 5!
      years_str = [str(i) for i in range(first_year,last_year,5)] # It's 5!


      , will ease the x axis information:



      a few dates on the x axis






      share|improve this answer

























        up vote
        1
        down vote



        accepted










        Matplotlib is putting the correct numbers but in scientific notation because there's no more room in the x-axis to put more stuff. That said you can interact directly with the x-axis and state what strings do you want, in what positions, using a value for rotation. The following example (data was randomized):



        import matplotlib.pyplot as chart
        from matplotlib import lines
        import random

        title = 'Title'
        first_year = 1960
        last_year = 2017
        x = [year for year in range(first_year,2017)]
        y1 = [random.randint(0,10) for year in range(first_year,2017)]
        y2 = [random.randint(0,10) for year in range(first_year,2017)]
        highest_value = max(y1+y2)

        # Settings
        chart.title(title)
        chart.xlabel('Years')
        chart.ylabel('Committers/Contributions')
        chart.ylim([0,highest_value + 0.05*highest_value])
        chart.xlim(first_year,last_year)

        # Values
        committer_line = chart.plot(x,y1,'r',label='Committer')
        contribution_line = chart.plot(x,y2,'b--',label='Contribution')
        years = list(range(first_year,last_year))
        years_str = [str(i) for i in range(first_year,last_year)]
        chart.xticks(years,years_str,rotation=45)

        # Legend
        chart.legend()

        # Show/Save
        #chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
        chart.show()


        , results in:



        lots of dates in x axis



        , which is a bit cluttered so giving a step to you "Label Code" range:



        years = list(range(first_year,last_year,5)) # It's 5!
        years_str = [str(i) for i in range(first_year,last_year,5)] # It's 5!


        , will ease the x axis information:



        a few dates on the x axis






        share|improve this answer























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Matplotlib is putting the correct numbers but in scientific notation because there's no more room in the x-axis to put more stuff. That said you can interact directly with the x-axis and state what strings do you want, in what positions, using a value for rotation. The following example (data was randomized):



          import matplotlib.pyplot as chart
          from matplotlib import lines
          import random

          title = 'Title'
          first_year = 1960
          last_year = 2017
          x = [year for year in range(first_year,2017)]
          y1 = [random.randint(0,10) for year in range(first_year,2017)]
          y2 = [random.randint(0,10) for year in range(first_year,2017)]
          highest_value = max(y1+y2)

          # Settings
          chart.title(title)
          chart.xlabel('Years')
          chart.ylabel('Committers/Contributions')
          chart.ylim([0,highest_value + 0.05*highest_value])
          chart.xlim(first_year,last_year)

          # Values
          committer_line = chart.plot(x,y1,'r',label='Committer')
          contribution_line = chart.plot(x,y2,'b--',label='Contribution')
          years = list(range(first_year,last_year))
          years_str = [str(i) for i in range(first_year,last_year)]
          chart.xticks(years,years_str,rotation=45)

          # Legend
          chart.legend()

          # Show/Save
          #chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
          chart.show()


          , results in:



          lots of dates in x axis



          , which is a bit cluttered so giving a step to you "Label Code" range:



          years = list(range(first_year,last_year,5)) # It's 5!
          years_str = [str(i) for i in range(first_year,last_year,5)] # It's 5!


          , will ease the x axis information:



          a few dates on the x axis






          share|improve this answer












          Matplotlib is putting the correct numbers but in scientific notation because there's no more room in the x-axis to put more stuff. That said you can interact directly with the x-axis and state what strings do you want, in what positions, using a value for rotation. The following example (data was randomized):



          import matplotlib.pyplot as chart
          from matplotlib import lines
          import random

          title = 'Title'
          first_year = 1960
          last_year = 2017
          x = [year for year in range(first_year,2017)]
          y1 = [random.randint(0,10) for year in range(first_year,2017)]
          y2 = [random.randint(0,10) for year in range(first_year,2017)]
          highest_value = max(y1+y2)

          # Settings
          chart.title(title)
          chart.xlabel('Years')
          chart.ylabel('Committers/Contributions')
          chart.ylim([0,highest_value + 0.05*highest_value])
          chart.xlim(first_year,last_year)

          # Values
          committer_line = chart.plot(x,y1,'r',label='Committer')
          contribution_line = chart.plot(x,y2,'b--',label='Contribution')
          years = list(range(first_year,last_year))
          years_str = [str(i) for i in range(first_year,last_year)]
          chart.xticks(years,years_str,rotation=45)

          # Legend
          chart.legend()

          # Show/Save
          #chart.savefig(images_path + file_name.replace('.txt','-commiter-contribution.eps'), format='eps')
          chart.show()


          , results in:



          lots of dates in x axis



          , which is a bit cluttered so giving a step to you "Label Code" range:



          years = list(range(first_year,last_year,5)) # It's 5!
          years_str = [str(i) for i in range(first_year,last_year,5)] # It's 5!


          , will ease the x axis information:



          a few dates on the x axis







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 '16 at 18:07









          armatita

          5,76042437




          5,76042437
























              up vote
              1
              down vote













              If you look in the lower right hand corner, you'll see that matplotlib simply changed the display of the xtick labels to use scientific notation to save space. To change this you will want to alter the tick label Formatter.



              To disable this conversion to scientific notation, you can tweak the default formatter:



              ax.get_xaxis().get_major_formatter().set_useOffset(False)


              This edits the useOffset parameter of your ScalarFormatter.



              If you want to disable this behavior by default, you can edit the axes.formatter.useoffset rcparam.






              share|improve this answer



























                up vote
                1
                down vote













                If you look in the lower right hand corner, you'll see that matplotlib simply changed the display of the xtick labels to use scientific notation to save space. To change this you will want to alter the tick label Formatter.



                To disable this conversion to scientific notation, you can tweak the default formatter:



                ax.get_xaxis().get_major_formatter().set_useOffset(False)


                This edits the useOffset parameter of your ScalarFormatter.



                If you want to disable this behavior by default, you can edit the axes.formatter.useoffset rcparam.






                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  If you look in the lower right hand corner, you'll see that matplotlib simply changed the display of the xtick labels to use scientific notation to save space. To change this you will want to alter the tick label Formatter.



                  To disable this conversion to scientific notation, you can tweak the default formatter:



                  ax.get_xaxis().get_major_formatter().set_useOffset(False)


                  This edits the useOffset parameter of your ScalarFormatter.



                  If you want to disable this behavior by default, you can edit the axes.formatter.useoffset rcparam.






                  share|improve this answer














                  If you look in the lower right hand corner, you'll see that matplotlib simply changed the display of the xtick labels to use scientific notation to save space. To change this you will want to alter the tick label Formatter.



                  To disable this conversion to scientific notation, you can tweak the default formatter:



                  ax.get_xaxis().get_major_formatter().set_useOffset(False)


                  This edits the useOffset parameter of your ScalarFormatter.



                  If you want to disable this behavior by default, you can edit the axes.formatter.useoffset rcparam.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 23 '16 at 18:12

























                  answered Mar 23 '16 at 18:07









                  Suever

                  53k134965




                  53k134965






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36184953%2fspecifying-values-for-my-x-axis-using-the-matplotlib-pyplot%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

                      Full-time equivalent

                      さくらももこ

                      13 indicted, 8 arrested in Calif. drug cartel investigation