我试图在条形图上绘制一条直线,但当我绘制直线时,条形图消失了——我的x索引值也消失了。谁能帮我在同一个图形中画出直线和条形图?非常感谢。我认为,我的问题是,线条图和条形图相互重叠,但我希望它们共存
plt.figure()
totalconfirm['Cases'].plot(kind='bar', rot=15, title="Cases per Day in all Michigan Counties", color='r', label = 'Confirmed Cases')
totalprob['Cases'].plot(kind = 'bar', rot=15, bottom=totalconfirm['Cases'], color = 'b', label = 'Probable Cases')
totalconfirm['Deaths'].plot(color = 'black', label = 'Deaths')
ax = plt.gca()
ax.xaxis.set_major_locator(plt.MaxNLocator(10))
ax.text(0.05, 0.95, "Total Tests: " + str(totaltotaltest), fontsize=10, transform=ax.transAxes,
verticalalignment='top', bbox = dict(boxstyle='round', facecolor='wheat', alpha=0.5))
ax.set_xticklabels(df.index.format(), rotation='vertical', size=5)
plt.locator_params(axis='x', nbins=30)
plt.legend(loc="upper right")
plt.show()
此外,如何将此文本框移动到图例下方的右侧
我已经看过了matplotlib plot bar and line charts together,Pandas plot bar chart over line它没有涵盖我想要的,我想
数据集:
Date
2020-03-01 0
2020-03-02 0
2020-03-03 0
2020-03-04 0
2020-03-05 0
2020-03-06 0
2020-03-07 0
2020-03-08 0
2020-03-09 0
2020-03-10 0
2020-03-11 0
2020-03-12 0
2020-03-13 0
2020-03-14 0
2020-03-15 0
2020-03-16 1
2020-03-17 0
2020-03-18 3
2020-03-19 5
2020-03-20 2
2020-03-21 8
2020-03-22 13
2020-03-23 21
2020-03-24 25
2020-03-25 24
2020-03-26 46
2020-03-27 49
2020-03-28 66
2020-03-29 62
2020-03-30 80
..
2020-06-19 11
2020-06-20 8
2020-06-21 8
2020-06-22 14
2020-06-23 9
2020-06-24 6
2020-06-25 8
2020-06-26 6
2020-06-27 11
2020-06-28 7
2020-06-29 7
2020-06-30 6
2020-07-01 7
2020-07-02 10
2020-07-03 9
2020-07-04 6
2020-07-05 5
2020-07-06 7
2020-07-07 10
2020-07-08 7
2020-07-09 8
2020-07-10 5
2020-07-11 7
2020-07-12 6
2020-07-13 4
2020-07-14 0
2020-07-15 6
2020-07-16 2
2020-07-17 3
2020-07-18 0
Name: Deaths, dtype: int64
Date
2020-03-01 14
2020-03-02 13
2020-03-03 22
2020-03-04 24
2020-03-05 26
2020-03-06 41
2020-03-07 48
2020-03-08 63
2020-03-09 122
2020-03-10 155
2020-03-11 200
2020-03-12 269
2020-03-13 360
2020-03-14 364
2020-03-15 462
2020-03-16 702
2020-03-17 766
2020-03-18 861
2020-03-19 738
2020-03-20 933
2020-03-21 784
2020-03-22 793
2020-03-23 1208
2020-03-24 1075
2020-03-25 1160
2020-03-26 1128
2020-03-27 1289
2020-03-28 1041
2020-03-29 893
2020-03-30 1532
...
2020-06-19 285
2020-06-20 189
2020-06-21 227
2020-06-22 398
2020-06-23 441
2020-06-24 419
2020-06-25 386
2020-06-26 368
2020-06-27 296
2020-06-28 304
2020-06-29 482
2020-06-30 481
2020-07-01 539
2020-07-02 470
2020-07-03 397
2020-07-04 273
2020-07-05 374
2020-07-06 605
2020-07-07 554
2020-07-08 607
2020-07-09 509
2020-07-10 478
2020-07-11 276
2020-07-12 280
2020-07-13 513
2020-07-14 382
2020-07-15 312
2020-07-16 228
2020-07-17 141
2020-07-18 8
Name: Cases, dtype: int64
我使用
plotly
完成了这项工作。我修改了Deaths
以增加该值,从而可以看到折线图数据帧:
图表:
使用熊猫绘图
更新: 使用日期作为索引
以
Date
作为索引的数据帧。绘制此图将得到与上面相同的图相关问题 更多 >
编程相关推荐