Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python timeseries绘图,如何在ts.plot()之外设置xlim和XTICK?_Python_Matplotlib_Pandas - Fatal编程技术网

python timeseries绘图,如何在ts.plot()之外设置xlim和XTICK?

python timeseries绘图,如何在ts.plot()之外设置xlim和XTICK?,python,matplotlib,pandas,Python,Matplotlib,Pandas,我知道我可以在熊猫绘图例程中设置xlim:ts.plot(xlim=…),但熊猫绘图完成后如何更改它 fig = plt.figure() ax = fig.gca() ts.plot(ax=ax) 有时是有效的,但如果熊猫将xaxis格式设置为从纪元开始的月份,而不是天,这将很难失败 有没有办法知道熊猫是如何将日期转换成xaxis,然后再以同样的方式转换我的xlim的 谢谢。如果我使用pd.Timestamp值设置x轴限制,这对我(熊猫0.16.2)是有效的 例如: ax.set_xlim(

我知道我可以在熊猫绘图例程中设置xlim:ts.plot(xlim=…),但熊猫绘图完成后如何更改它

fig = plt.figure()
ax = fig.gca()
ts.plot(ax=ax)
有时是有效的,但如果熊猫将xaxis格式设置为从纪元开始的月份,而不是天,这将很难失败

有没有办法知道熊猫是如何将日期转换成xaxis,然后再以同样的方式转换我的xlim的

谢谢。

如果我使用
pd.Timestamp
值设置x轴限制,这对我(熊猫0.16.2)是有效的

例如:

ax.set_xlim(( t0.toordinal(), t1.toordinal() )
结果:


请注意,如果在同一个图形中绘制多个时间序列,请确保在最后一个
ts.plot()
命令之后设置xlim/ylim,否则熊猫将自动重置限制以匹配内容。

关于后面的列表位很重要!谢谢Matplotlib太挑剔了。
import pandas as pd

# Create a random time series with values over 100 days
# starting from 1st March.
N = 100
dates = pd.date_range(start='2015-03-01', periods=N, freq='D')
ts = pd.DataFrame({'date': dates,
                   'values': np.random.randn(N)}).set_index('date')

# Create the plot and adjust x/y limits. The new x-axis
# ranges from mid-February till 1st July.
ax = ts.plot()
ax.set_xlim(pd.Timestamp('2015-02-15'), pd.Timestamp('2015-07-01'))
ax.set_ylim(-5, 5)