首页 > 解决方案 > 覆盖 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

感谢您的帮助!

标签: pythonmatplotlibplotscatter

解决方案


没关系,错误不是就地重新索引:

df_2015_max = df_2015_max.reindex(df_max.index)
df_2015_min = df_2015_min.reindex(df_min.index)

解决它。


推荐阅读