I am trying to fix how python plots my data. Say:
x = [0, 5, 9, 10, 15]
y = [0, 1, 2, 3, 4]
matplotlib.pyplot.plot(x, y)
matplotlib.pyplot.show()
The x axis’ ticks are plotted in intervals of 5. Is there a way to make it show intervals of 1?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could explicitly set where you want to tick marks with
plt.xticks:For example,
(
np.arangewas used rather than Python’srangefunction just in casemin(x)andmax(x)are floats instead of ints.)The
plt.plot(orax.plot) function will automatically set defaultxandylimits. If you wish to keep those limits, and just change the stepsize of the tick marks, then you could useax.get_xlim()to discover what limits Matplotlib has already set.The default tick formatter should do a decent job rounding the tick values to a sensible number of significant digits. However, if you wish to have more control over the format, you can define your own formatter. For example,
Here’s a runnable example: