Home > Software design >  Xticks different interval
Xticks different interval

Time:06-29

How do i set xticks to 'a different interval'

For instance:

plt.plot(1/(np.arange(0.1,3,0.1)))

returns:

enter image description here

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:

enter image description here

CodePudding user response:

You want to learn about plt.xlim and adjacent functions.

plt.plot(1/(np.arange(0.1,3,0.1)))
plt.xlim(0, 3)
plt.show()

figure

CodePudding user response:

You can use numpy.arange() to get the desired range with a specific step:

import matplotlib.pyplot as plt
import numpy as np

y = 1/(np.arange(0.1,3,0.1))
plt.tight_layout()
plt.plot(y)
plt.xticks(np.arange(0, len(y), 6), [str(round(i, 2)) for i in np.arange(0, 3, (3*6)/len(y))])
plt.show()

enter image description here Also, you can see more examples of xticks() on the official matplotlib documentation.

  • Related