Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I am trying to fix how python plots my data.

Say

x = [0,5,9,10,15]

and

y = [0,1,2,3,4]

Then I would do:

matplotlib.pyplot.plot(x,y)

matplotlib.pyplot.show()

and the x-axis' ticks are plotted in intervals of 5. Is there a way to make it show intervals of 1?

1 Answer

0 votes
by (106k points)

For changing the “tick frequency” on x or y-axis in matplotlib you can explicitly set where you want to tick marks with plt.xticks. Below is an example that shows how to do it:-

plt.xticks(np.arange(min(x), max(x)+1, 1.0))

An example that illustrates the use of plt.xticks:-

import numpy as np

import matplotlib.pyplot as plt

x = [0,5,9,10,15]

y = [0,1,2,3,4]

plt.plot(x,y)

plt.xticks(np.arange(min(x), max(x)+1, 1.0))

plt.show()

The np.arange function was used in place of Python's range function just in case min(x) and max(x) are floats instead of ints.

Related questions

0 votes
1 answer
asked Jul 18, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...