【问题标题】:Matplotlib df.dataframe x axis showing month and dayMatplotlib df.dataframe x 轴显示月份和日期
【发布时间】:2021-12-22 14:49:34
【问题描述】:

我的 x 轴长度为 168(7 天 * 24 小时)。我想展示 1 月 4 日、1 月 5 日……1 月 10 日),但似乎找不到如何做到这一点。

plt.plot(t, php) 
plt.title('Electricity Load of heat pump at t')

fig, axes = plt.subplots(figsize=[25,7])
fig.subplots_adjust(bottom=0.2) 
tindex = np.arange(0,T)

dates = pd.date_range('20210104',periods=168, freq="H")

df = pd.DataFrame({'php1': php, 'dates': dates})

df.plot(kind='line',x='dates', y='php1',
        ax=axes, color ="red", linestyle='-', 
        linewidth=3, grid=True)
             
axes.tick_params(axis='x', labelsize=15)
axes.tick_params(axis='y', labelsize=15) 
axes.get_legend().remove()
axes.set_ylabel('Power[kW]', fontsize=15)
axes.set_xlabel('Timeslot[Days] ', fontsize=15)

【问题讨论】:

  • 现有的答案是否让您满意?我认为答案中的代码效果不佳。

标签: pandas dataframe matplotlib


【解决方案1】:

试试这个:

import matplotlib.dates as mdates

mdate = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(mdate)

【讨论】:

  • 谢谢你,但它似乎并不奏效..
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多