Skip to content Skip to sidebar Skip to footer

Specifying Values For My X-axis Using The Matplotlib.pyplot?

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-a

Solution 1:

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 inrange(first_year,2017)]
y1 = [random.randint(0,10) for year inrange(first_year,2017)]
y2 = [random.randint(0,10) for year inrange(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 inrange(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

Solution 2:

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.

Post a Comment for "Specifying Values For My X-axis Using The Matplotlib.pyplot?"