【发布时间】:2018-08-06 09:15:40
【问题描述】:
我使用 python 的 matplotlib 绘制了一个绘图,它在一个共享的 x 轴上绘制了一个包含 1000 个不同每日值序列的数据框。 x 轴代表日期,从 1985 年 1 月 1 日到 1985 年 12 月 31 日,但年份无关紧要,仅用于确保所有年份的数据都覆盖在同一轴上。当用月轴绘制时,我无法避免在第一个“Jan”刻度下显示 1985。这既丑陋又烦人,因为没有一个数据代表 1985 年(它们是从合成输入数据生成的计算结果)。
如何去掉第一个 x 轴刻度上的年份指示器?我可以为不包含年份的数据框定义日期索引吗?只是mmm-dd?甚至一些用白盒子或其他东西覆盖这一年的小贴士也会有所帮助。
用于定义图形的代码如下。我知道这是丑陋的脚本,但它可以满足我的需求....:
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(11,8))
RSEs.plot(ax=axes[0,0], legend=False, title="Reservoir Elevation")
TBFs.plot(ax=axes[0,1], legend=False, title="Turbine Flow")
TSs.plot(ax=axes[1,0], legend=False, title="Total Spill")
OTs.plot(ax=axes[1,1], legend=False, title="Overtopping Flow")
l1=RLIMS[LimitCols[1]].plot(ax=axes[0,0], color="k", linestyle=":", legend=False)
l2=RLIMS[LimitCols[0]].plot(ax=axes[0,0], color="k", linestyle=":", legend=False)
l3=RLIMS[LimitCols[2]].plot(ax=axes[0,0], color="k", linestyle="--", legend=False)
l4=RLIMS[LimitCols[3]].plot(ax=axes[0,0], color="k", linestyle="--", legend=False)
#fig.autofmt_xdate()
axes[1,0].fmt_xdata = mdates.DateFormatter("%m")
axes[1,1].fmt_xdata = mdates.DateFormatter("%m")
axes[0,0].set_ylabel('Reservoir Elevation (m)', fontsize=14)
axes[0,1].set_ylabel('Turbine Flow (m$^3$/s)', fontsize=14)
axes[1,0].set_ylabel('Gated Spill Release (m$^3$/s)', fontsize=14)
axes[1,1].set_ylabel('Free Overflow Spill (m$^3$/s)', fontsize=14)
axes[0,0].annotate('(a)', xy=(0.92, 0.9), xycoords='axes fraction', fontsize=14, horizontalalignment='top', verticalalignment='left')
axes[0,1].annotate('(b)', xy=(0.92, 0.9), xycoords='axes fraction', fontsize=14, horizontalalignment='top', verticalalignment='left')
axes[1,0].annotate('(c)', xy=(0.92, 0.9), xycoords='axes fraction', fontsize=14, horizontalalignment='top', verticalalignment='left')
axes[1,1].annotate('(d)', xy=(0.92, 0.9), xycoords='axes fraction', fontsize=14,
horizontalalignment='top', verticalalignment='left')
axes[0,1].yaxis.set_label_coords(-0.115,0.5)
axes[1,0].yaxis.set_label_coords(-0.122,0.5)
日期的定义如下所示,用于为包含 1000 列不同的综合驱动的每日数据的数据框定义索引值。
dates=pd.DatetimeIndex(start="1985-01-01", end="1985-12-31", freq="D")
编辑
以下是数据框的内容......rse、tbf、ts和@987654329 @ 是尺寸为 365 x 1000 的简单数组。
Cols=np.arange(1,1001)
RSEs=pd.DataFrame(rse, index=dates, columns=Cols)
TBFs=pd.DataFrame(tbf, index=dates, columns=Cols)
TSs=pd.DataFrame(ts, index=dates, columns=Cols)
OTs=pd.DataFrame(ot, index=dates, columns=Cols)
【问题讨论】:
-
我担心您可能需要直接使用 matplotlib 进行绘图,以便您可以使用自定义格式化程序。 Pandas 擅长自动标注,但不能很好地/根本无法定制。
-
请显示RSEs、TBFs、TSs、OTs的内容,以便我们见索引。
标签: python pandas matplotlib