如何调整Matplotlib中的X轴

3

我有一个类似这样的图表enter image description here

图表中的x轴表示小时,因此我想将x轴设置为0、24、48、72...而不是现在的值,这样可以更好地看到[0,100]之间的数据。

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

根据第一个答案,我得到了这个:输入图像描述
3个回答

4

Look at the docs :

xlocs, xlabs = plt.xticks()

将您的范围放入xlocs中,在xlabs中输入您想要显示的内容。
然后:
 plt.xticks(xlocs, xlabs)

1
谢谢,但我不认为它解决了我的问题,这个xticks只改变了标签,我上传了新的图表,我希望数据能够更散乱地显示,而不仅仅是标签。 - manxing
从您展示的图表来看,似乎您错误地使用了xticks,您应该改为像这样做: plt.xticks([0,24,48,72,96,120],[0,24,48,72,96,120]) - sega_sai

4

看起来你想要改变绘图显示的限制 - 为此使用 xlim(对于另一个轴使用ylim)。要更改xticks本身,可以参考@fp的答案。下面是一个示例,使用了没有/有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


0

通过设置Matplotlib的动态rc设置,可以更改绘图的大小。这些设置存储在名为rcParams的字典中。绘图图形的大小存储在键figure.figsize中。

例如,要获取和设置Matplotlib绘图的大小: 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接