【问题标题】:How to get rid of the year in my monthly plot (matplotlib)如何摆脱我每月情节中的年份(matplotlib)
【发布时间】: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")

编辑
以下是数据框的内容......rsetbfts@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 擅长自动标注,但不能很好地/根本无法定制。
  • 请显示RSEsTBFsTSsOTs的内容,以便我们见索引。

标签: python pandas matplotlib


【解决方案1】:

mmm-dd

最好通过 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' ) )

【讨论】:

  • 我收到错误:NameError: name 'DateFormatter' is not defined 当我将它添加到代码中时。有什么我需要导入的吗?我也只想要 x 轴上的刻度表示 mmm(没有天,我正在寻找显示多年每日数据的月度图)
  • import ... ?绝对matplotlib.dates.DateFormatter( "%b" )(文档在这些细节中非常清楚+许多额外的技巧,请不要犹豫阅读它)
  • 当我添加“from matplotlib.dates import DateFormatter”时它起作用了。谢谢。
猜你喜欢
  • 2021-12-02
  • 2011-09-08
  • 2021-02-15
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 2015-02-13
  • 2015-09-28
  • 1970-01-01
相关资源
最近更新 更多