Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to adjust x axis in matplotlib

I have a graph like thisenter image description here

The data on the x axis means hours, so I want the x axis to set as 0, 24, 48, 72.....instead of the value now, which is difficult to see the data between [0,100]

fig1 = plt.figure()  
ax = fig1.add_subplot(111)
ax.set_yscale('log')
ax.plot(x,y,'b-')

According to the the first answer, I got this: enter image description here

like image 479
manxing Avatar asked Sep 06 '26 13:09

manxing


2 Answers

Look at the docs :

xlocs, xlabs = plt.xticks()

put in xlocs your range, and in xlabs what you want to display.

then:

 plt.xticks(xlocs, xlabs)
like image 116
f p Avatar answered Sep 08 '26 02:09

f p


It sounds like you want to changes the limits of the plotting display - for that use xlim (and ylim for the other axis). To change the xticks themselves is provided in the answer by @fp. Show below is an example using without/with xlim:

# Sample data
import numpy as np
N = 2000
X = np.random.gamma(.5,size=N)*100

import pylab as plt

plt.subplot(2,1,1)
plt.hist(X,bins=300)

plt.subplot(2,1,2)
plt.hist(X,bins=300)
plt.xlim(0,100)

plt.show()

enter image description here

like image 30
Hooked Avatar answered Sep 08 '26 01:09

Hooked



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!