【发布时间】:2021-06-15 13:00:34
【问题描述】:
我正在尝试为我的一个系统制作一个组合图。我正在尝试使用条形图和折线图(带步骤)来可视化时间序列数据。 这是一个时间序列数据,因此我想在 x 标签中显示日期时间。
当我使用 df2['datetime'].index 绘制条形图时,我得到了预期的绘图,但我没有得到日期时间作为 x-ticks。我相信这是因为我使用的是 df2['datetime'].index。
当我使用 df2['datetime'] 绘制条形图时,我得到了 x-ticks 中的日期时间,但绘图看起来并不真实。
我还附上了我的数据快照以便更好地理解。任何建议都会很有用。谢谢。
fig,(ax0,ax1) = plt.subplots(1,2, figsize = (24,12))
ax0.bar(df2['datetime'].index, df2['T1'], width = 1, color='blue', align = 'edge', alpha = 0.5, label ='T1')
ax0.bar(df2['datetime'].index, -1*df2['T2'], width = 1, color='red', align = 'edge',alpha = 0.5, label ='T2')
ax0.step(df2['datetime'].index, df2['T3'], color='blue', linewidth=2, where = 'post', label ='T3')
ax0.step(df2['datetime'].index, -1*df2['T4'], color='red', linewidth=2, where = 'post', label ='T4')
ax00 = ax0.twinx()
ax00.step(df2['datetime'].index, df2['A1'], color='b', linestyle = '--', linewidth=1, where = 'post', label ='A1')
ax00.step(df2['datetime'].index, df2['A2'], color='r', linestyle = '--', linewidth=1, where = 'post', label ='A2')
ax00.set_ylabel('L2', fontsize=12, color='black')
ax0.set_ylabel("L1", fontsize=12, color='black')
ax0.set_xlabel("Datetime", fontsize=12, color='black')
ax0.set_title('Zone 2', fontsize=16, color='black')
ax0.grid(True)
ax0.legend(loc='upper left',fontsize = 12)
ax00.legend(loc='upper right',fontsize = 12)
ax1.bar(df2['datetime'], df2['T1'], width = 1, color='blue', align = 'edge', alpha = 0.5, label ='T1')
ax1.bar(df2['datetime'], -1*df2['T2'], width = 1, color='red', align = 'edge',alpha = 0.5, label ='T2')
ax1.step(df2['datetime'], df2['T3'], color='blue', linewidth=2, where = 'post', label ='T3')
ax1.step(df2['datetime'], -1*df2['T4'], color='red', linewidth=2, where = 'post', label ='T4')
ax01 = ax1.twinx()
ax01.step(df2['datetime'], df2['A1'], color='b', linestyle = '--', linewidth=1, where = 'post', label ='A1')
ax01.step(df2['datetime'], df2['A2'], color='r', linestyle = '--', linewidth=1, where = 'post', label ='A2')
ax01.set_ylabel('L2', fontsize=12, color='black')
ax1.set_ylabel("L1", fontsize=12, color='black')
ax1.set_xlabel("Datetime", fontsize=12, color='black')
ax1.set_title('Zone 1', fontsize=16, color='black')
ax1.grid(True)
ax1.legend(loc='upper left',fontsize = 12)[![enter image description here][1]][1]
ax01.legend(loc='upper right',fontsize = 12)
plt.show()
【问题讨论】:
标签: python pandas matplotlib bar-chart visualization