Issue
How do i set xticks to 'a different interval'
For instance:
plt.plot(1/(np.arange(0.1,3,0.1)))
returns:
If I would like the x axis to be on a scale from 0 to 3, how can i do that? I've tried
plt.xticks([0,1,2])
but that returns:
Solution
You want to learn about plt.xlim
and adjacent functions. This causes the X axis to have limits (minimum, maximum) that you specify. Otherwise Matplotlib decides for you based on the values you try to plot.
y = 1 / np.arange(0.1,3,0.1)
plt.plot(y)
plt.xlim(0, 3) # minimum 0, maximum 3
plt.show()
Your plot uses only Y values, so the X values are automatically chosen to be 1, 2, 3, ... to pair up with each Y value you provide.
If you desire to determine the X too, you can do that:
x = np.arange(0.1,3,0.1)
y = 1/x
plt.plot(x, y)
plt.xticks([0,1,2,3]) # ticks at those positions, if you don't like the automatic ones
plt.show()
Answered By - Christoph Rackwitz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.