Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python matplotlib - setting x-axis scale

I have this graph displaying the following:

plt.plot(valueX, scoreList)
plt.xlabel("Score number") # Text for X-Axis
plt.ylabel("Score") # Text for Y-Axis
plt.title("Scores for the topic "+progressDisplay.topicName)
plt.show()

valueX = [1, 2, 3, 4] and scoreList = [5, 0, 0, 2]

I want the scale to go up in 1's, no matter what values are in 'scoreList'. Currently get my x-axis going up in .5 instead of 1s.

How do I set it so it goes up only in 1?

like image 767
YusufChowdhury Avatar asked Apr 07 '17 20:04

YusufChowdhury


People also ask

How do you change the X-axis scale in MatPlotLib?

To change the range of X and Y axes, we can use xlim() and ylim() methods.

How do you change the size of the X-axis in Python?

If we want to change the font size of the axis labels, we can use the parameter “fontsize” and set it your desired number.


3 Answers

Just set the xticks yourself.

plt.xticks([1,2,3,4])

or

plt.xticks(valueX)

Since the range functions happens to work with integers you could use that instead:

plt.xticks(range(1, 5))

Or be even more dynamic and calculate it from the data:

plt.xticks(range(min(valueX), max(valueX)+1))
like image 143
Novel Avatar answered Oct 18 '22 20:10

Novel


Below is my favorite way to set the scale of axes:

plt.xlim(-0.02, 0.05)
plt.ylim(-0.04, 0.04)
like image 32
Amin Heydari Alashti Avatar answered Oct 18 '22 21:10

Amin Heydari Alashti


Hey it looks like you need to set the x axis scale.

Try

matplotlib.axes.Axes.set_xscale(1, 'linear')

Here's the documentation for that function

like image 8
dustyjuicebox Avatar answered Oct 18 '22 19:10

dustyjuicebox