python - 覆盖 2 个具有相同索引的 DataSeries 图
问题描述
我提取了 2015 年的温度数据并创建了 2 个 DataSeries,用于创纪录的高点和低点(df_2015_max 和 df_2015_min),其中 df_min 和 _max 是 10 年期间的创纪录低点和高点。这些系列分别有 37 和 32 个值。
df_2015_min = df_2015_min[df_2015_min < df_min]
df_2015_max = df_2015_max[df_2015_max > df_max]
然后我重新索引了这些系列以匹配 df_min 和 _max 的索引,它们是最低和最高温度。因此,只要记录没有被破坏,我就有 NaN 值。
df_2015_max.reindex(df_max.index)
df_2015_min.reindex(df_min.index)
我认为如果我将这些绘制到现有的情节中,它会自动正确地索引它,但这不会发生,我不知道如何修复它。
plt.figure()
#plot data
ax1 = df_min.plot(label = "_nolegend_")
ax2 = df_max.plot(label = "_nolegend_")
df_2015_max.plot(marker = "o", label ="Record Breaking High", ax = ax2, linestyle = "none")
df_2015_min.plot(marker = "o", label ="Record Breaking Low", ax = ax1, linestyle = "none")
#fill_between data
plt.gca().fill_between(range(365), df_min, df_max, facecolor = "purple", alpha = 0.1)
#set axes labels and figure title
plt.title("Temperature record highs and lows (2004 - 2015) in °C")
#set xticks
plt.tick_params(left='off', bottom='on', labelleft='on', labelbottom='on')
ax1.set_xticks(range(0,360, 30)) #set approximate ticks for months in a year
#list comprehension, take each month from a year and format it as abbreviation (%b)
ax1.set_xticklabels([datetime(2010,i,1).strftime('%b') for i in range(1,13)])
#add legend
plt.legend()
plt.show()
但结果绘制的是从年初开始的值,而不是对应日期的值: Plot
感谢您的帮助!
解决方案
没关系,错误不是就地重新索引:
df_2015_max = df_2015_max.reindex(df_max.index)
df_2015_min = df_2015_min.reindex(df_min.index)
解决它。
推荐阅读
- c - Readline C:强制返回 readline() 中的某些文本
- angular - Angular 6:如何从 observable 返回数据?
- android - 模拟器因 -enable-whpx 而失败
- raspberry-pi - 关于树莓派 3 功耗
- r - 在每列上使用带有颜色分组的构面
- java - 如何在将打印流从 XSSF XLSX2CSV 类转换为 ByteInputStream 时保留换行符?
- javascript - 异步还是同步?我应该如何处理这些 API 调用?
- javascript - 浏览器通知未出现
- javascript - 如何使用 *ngFor 循环的角度在表格字段内使用 *ngIf 条件?
- angular - 角材料导航示意图断点