【发布时间】:2020-12-03 11:55:52
【问题描述】:
我有以下熊猫数据框:
NAME | 2020-12-03 12:00| 2020-12-03 15:00| 2020-12-03 18:00| 2020-12-03 21:00| etc.
London| 5 | 4 | 3.6 | 1.7 | ...
Berlin| 4 | 4.5 | 2.8 | 0.1 | ...
etc.
它基本上是一个带有多个城市和列的长表,带有°C,标题是相应的时间戳。我现在想计算每个城市每天汇总的最低和最高温度。决赛桌可能如下所示:
NAME |Minimum | Maximum |timestamp |
London | 1.7 | 5 |2020-12-03|
Berlin | 4.5 | 0.1 |2020-12-03|
为了让事情变得更加复杂,我想用 matplotlib 为每个城市绘制图表,并将最小值和最大值作为每个时间戳的条形图。所以我不确定决赛桌是否应该像上面那样。
我已经尝试过转置表格并按时间戳分组(由于无法将列标题设置为日期时间值,因此无法正常工作)。 我可以使用以下脚本很好地打印出第一个表的值,但如前所述,我想获取最小值和最大值。
for i in range(0, fcpanda3.shape[0]):
plt.rcParams["figure.figsize"] = (15,15)
ax = fcpanda3.iloc[i].T.plot(kind="bar", color=(fcpanda3.iloc[i].T > 0).map({True: 'r',False: 'b'}))
ax.set_xticklabels([t if not i%5 else "" for i,t in enumerate(ax.get_xticklabels())])
ax.yaxis.set_major_formatter(FormatStrFormatter('%d °C'))
plt.tight_layout()
plt.savefig("D:/graph/"+str(i+1)+".png")
ax = plt.close()
【问题讨论】:
标签: python python-3.x pandas matplotlib