Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xticks different interval

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

like image 999
DHJ Avatar asked Oct 19 '25 04:10

DHJ


1 Answers

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()

figure 1

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()

figure 2

like image 132
Christoph Rackwitz Avatar answered Oct 21 '25 18:10

Christoph Rackwitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!