nwl*_*wly 7 python date matplotlib
我正在绘制一个默认格式显示为的图形:
我想修改它,以便每月1个月出现月份,但保留年份.我目前的尝试是这样的:
years = mdates.YearLocator()
months = mdates.MonthLocator()
monthsFmt = mdates.DateFormatter('%b-%y')
dts = s.index.to_pydatetime()
fig = plt.figure(); ax = fig.add_subplot(111)
ax.plot(dts, s)
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(monthsFmt)
Run Code Online (Sandbox Code Playgroud)
但它没有产生正确的结果:
我究竟需要如何修改它以便它像第一个一样出现但每个月都会出现月份?
nwl*_*wly 10
想出一个解决方案,就是坚持几个月进入小蜱并保持多年为主要.
例如
years = mdates.YearLocator()
months = mdates.MonthLocator()
monthsFmt = mdates.DateFormatter('%b')
yearsFmt = mdates.DateFormatter('\n\n%Y') # add some space for the year label
dts = s.index.to_pydatetime()
fig = plt.figure(); ax = fig.add_subplot(111)
ax.plot(dts, s)
ax.xaxis.set_minor_locator(months)
ax.xaxis.set_minor_formatter(monthsFmt)
plt.setp(ax.xaxis.get_minorticklabels(), rotation=90)
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yearsFmt)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3933 次 |
| 最近记录: |