简体   繁体   中英

how to adjust x axis in matplotlib

I have a graph like this 在此处输入图片说明

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: 在此处输入图片说明

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)

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

在此处输入图片说明

The size of the plot can be changed by setting the dynamic rc settings of Matplotlib. These are stored in a dictionary named rcParams. The size of the plot figure is stored with the key figure.figsize.

As an example, to get and set the size of a Matplotlib plot: import matplotlib.pyplot as plt

fig_size = plt.rcParams["figure.figsize"] #Get current size
print "Current size:", fig_size           #Prints: [8.0, 6.0]
fig_size[0] = 12                          #Set figure width to 12 and height to 9
fig_size[1] = 9
plt.rcParams["figure.figsize"] = fig_size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM