如何在我的月图(matplotlib)中去掉年

2024-10-05 14:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python的matplotlib绘制了一个图,它在一个共享的x轴上绘制了一个包含1000个不同的日值序列的数据帧。x轴表示日期,从1985年1月1日到1985年12月31日,但年份不相关,仅用于确保所有年份的数据都覆盖在同一轴上。当用月坐标轴绘制时,我无法避免在第一个“一月”记号下显示1985年。这是丑陋和恼人的,因为没有一个数据是1985年的代表(他们是从合成输入数据产生的计算结果)。你知道吗

enter image description here

如何去掉第一个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")

编辑 以下是数据帧的内容。。。。rsetbftsot是尺寸为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)

Tags: 数据false定义plottitleaxpddates
1条回答
网友
1楼 · 发布于 2024-10-05 14:29:06

只是mmm-dd?你知道吗

Best可以通过X轴调整适当的设置,其中DateFormatter( fmt, tz = None ),其中fmt将设置strftime-格式化代码:

"%b-%d" # WARNING: %b produced content is locale-specific,
        #             so review a code in cases, where international deliveries
        #             may take place in the future

axes[i,j].xaxis.set_major_formatter( DateFormatter( '%b-%d' ) )

相关问题 更多 >