Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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 Pandas timeseries打印设置x轴长、短刻度和标签_Python_Matplotlib_Pandas - Fatal编程技术网

Python Pandas timeseries打印设置x轴长、短刻度和标签

Python Pandas timeseries打印设置x轴长、短刻度和标签,python,matplotlib,pandas,Python,Matplotlib,Pandas,我希望能够为从Pandas时间序列对象绘制的时间序列图设置主要和次要XTICK及其标签 熊猫0.9“最新信息”页面显示: “您可以使用_pydatetime或为 “时间戳类型” 但是我不知道如何使用matplotlibax.xaxis.set_major\u定位器和ax.xaxis.set_major\u格式化程序(和minor)命令 如果我在不转换时间的情况下使用它们,则x轴的记号和标签最终会出错 通过使用“xticks”参数,我可以将主刻度传递给pandas.plot,然后设置主刻度标签。我

我希望能够为从Pandas时间序列对象绘制的时间序列图设置主要和次要XTICK及其标签

熊猫0.9“最新信息”页面显示:

“您可以使用_pydatetime或为 “时间戳类型”

但是我不知道如何使用matplotlib
ax.xaxis.set_major\u定位器
ax.xaxis.set_major\u格式化程序
(和minor)命令

如果我在不转换时间的情况下使用它们,则x轴的记号和标签最终会出错

通过使用“xticks”参数,我可以将主刻度传递给pandas.plot,然后设置主刻度标签。我不知道如何使用这种方法进行小刻度。(我可以在pandas.plot设置的默认小刻度上设置标签)

以下是我的测试代码:

导入熊猫
打印“pandas.\uuuu版本”\uuuuu是“pandas.\uuu版本”__
打印“matplotlib.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
dStart=datetime.datetime(2011,5,1)#5月1日
dEnd=日期时间。日期时间(2011,7,1)#7月1日
dateIndex=pandas.date_范围(开始=dStart,结束=dEnd,频率=D')
打印“2011年5月1日至7月1日”,日期索引
testSeries=pandas.Series(data=np.random.randn(len(dateIndex)),
索引=日期索引)
ax=plt.图(figsize=(7,4),dpi=300)。添加子批次(111)
plot(ax=ax,style='v-',label='first line')
#使用MatPlotLib日期-时间定位器和格式设置器不适用于新的
#日期时间索引
ax.xaxis.set_minor_定位器(matplotlib.dates.WeekdayLocator)(byweekday=(1),
间隔=1)
ax.xaxis.set\u minor\u格式化程序(matplotlib.dates.DateFormatter(“%d\n%a”))
ax.xaxis.grid(True,which=“minor”)
ax.xaxis.grid(False,which=“major”)
ax.xaxis.set\u major\u格式化程序(matplotlib.dates.DateFormatter('\n\n\n%b%Y'))
plt.show()
#通过大熊猫设置主要的XTICK和标签
ax2=plt.图(figsize=(7,4),dpi=300)。添加子图(111)
xticks=pandas.date_范围(开始=dStart,结束=dEnd,频率=W-Tue)
打印“xticks:”,xticks
plot(ax=ax2,style='-v',label='second line',
xticks=xticks.to_pydatetime())
ax2.在xtick中为x设置xticklabel([x.strftime(“%a\n%d\n%h\n%Y”);
#设置pandas.plot创建的前几个小刻度的文本
#ax2.set_xticklabels(['a','b','c','d','e',minor=True)
#删除pandas.plot设置的次要xtick标签
ax2.set_xticklabels([],minor=True)
#关闭pandas.plot创建的次要刻度
#plt.minorticks_off()
plt.show()
打印测试系列['6/4/2011':'6/7/2011']
其输出:

pandas.__version__ is  0.9.1.dev-3de54ae
matplotlib.__version__ is  1.1.1
1 May to 1 July 2011 <class 'pandas.tseries.index.DatetimeIndex'>
[2011-05-01 00:00:00, ..., 2011-07-01 00:00:00]
Length: 62, Freq: D, Timezone: None
更新:通过使用循环构建主要的xtick标签,我能够更接近我想要的布局:

# only show month for first label in month
month = dStart.month - 1
xticklabels = []
for x in xticks:
    if  month != x.month :
        xticklabels.append(x.strftime('%d\n%a\n%h'))
        month = x.month
    else:
        xticklabels.append(x.strftime('%d\n%a'))

但是,这有点像使用ax注释x轴:可能但不理想。
pandas
matplotlib.dates
使用
matplotlib.units
定位刻度

但是,虽然
matplotlib.dates
提供了手动设置刻度的便捷方法,但到目前为止,pandas似乎专注于自动格式化(您可以查看pandas中的日期转换和格式化功能)

因此,目前使用
matplotlib.dates
(正如@BrenBarn在他的评论中提到的)似乎更为合理


(我的地区是德语,所以星期二(星期二)变成了迪恩斯塔格(Dienstag)[Di])

我知道这并不能真正回答这个问题,但作为一种一般方法,当我真正关心一个情节的外观时,我通常只会尝试获得它的矢量版本,并使它在Illustrator或Inkscape中看起来漂亮。我发现我认识的大多数其他人似乎也这么做。你能完全忽略pandas
plot
函数的参数,并在打印后使用返回的
ax
对象(例如,
ax.set\xticks
)的matplotlib方法设置所有刻度吗?@BrenBarn对于matplotlib方法,我不知道如何将日期作为python日期而不是pandas日期时间。bmu的答案通过在打印前转换日期来修复这一问题。实际上,您可以使用pandas打印,并且仍然使用matplotlib.dates,而不需要任何日期转换,这要归功于以下参数:
testSeries.plot(x\u compat=True)
。这是在你发布这个问题后的几个星期。
2011-06-04   -0.199393
2011-06-05   -0.043118
2011-06-06    0.477771
2011-06-07   -0.033207
Freq: D
# only show month for first label in month
month = dStart.month - 1
xticklabels = []
for x in xticks:
    if  month != x.month :
        xticklabels.append(x.strftime('%d\n%a\n%h'))
        month = x.month
    else:
        xticklabels.append(x.strftime('%d\n%a'))
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
import matplotlib.dates as dates

idx = pd.date_range('2011-05-01', '2011-07-01')
s = pd.Series(np.random.randn(len(idx)), index=idx)

fig, ax = plt.subplots()
ax.plot_date(idx.to_pydatetime(), s, 'v-')
ax.xaxis.set_minor_locator(dates.WeekdayLocator(byweekday=(1),
                                                interval=1))
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d\n%a'))
ax.xaxis.grid(True, which="minor")
ax.yaxis.grid()
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n\n%b\n%Y'))
plt.tight_layout()
plt.show()