31

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?

3 Answers 3

43

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))
Sign up to request clarification or add additional context in comments.

Comments

22

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

plt.xlim(-0.02, 0.05)
plt.ylim(-0.04, 0.04)

Comments

9

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

1 Comment

This is incorrect in matplotlib 3. It should read plt.gca().set_xscale('linear'). Otherwise you'll get AttributeError: 'function' object has no attribute 'Axes' from writing plt.axes.Axes and TypeError: set_xscale() takes 2 positional arguments but 3 were given from writing set_xscale(1, 'linear').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.