Reputation:
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:
A case that does not works:
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()
Upvotes: 1
Views: 4765
Reputation: 65430
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.
Upvotes: 1
Reputation: 13465
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:
, 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:
Upvotes: 1