【问题标题】:Need Assistance: Unable to get datetime as x ticks with bar chart需要帮助:无法以条形图的 x 刻度获取日期时间
【发布时间】: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


    【解决方案1】:

    问题可能来自width = 1,因为使用日期时间,宽度为 1 不等于两个数据点之间的大小。

    尽量使用较小的宽度

    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')
    

    你也可以计算出你需要的确切宽度,但我会让你搜索如何做到这一点;)

    【讨论】:

    • 右:width=1 表示一天。你可能想要width=1/24,或者width=np.timedelta64(1, 'H') 也应该可以。
    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多