我是新手,正在尝试在图表上.fill_ Between()
绘制两行。我已经尝试了所有我能想到的方法来确保基础数据正确,但每次我绘制图表时,.fill_ Between()
在 x 轴上都会出现一个比应有的值短的值。
这是我的代码:
df_combo = pd.read_csv('MySampleData.csv')
max_hist_temps = df_combo['hist_max']
min_hist_temps = df_combo['hist_min']
plt.figure()
plt.plot(max_hist_temps, '-.r'
, min_hist_temps, '-.b'
)
plt.gca().fill_between(range(len(max_hist_temps)),
min_hist_temps, max_hist_temps,
facecolor='blue',
alpha = 0.25)
ax = plt.gca()
ax.axis([0,364,-45,45])
plt.show()
这是一些示例数据:
Day_of_Year hist_min hist_max
1 -16 15.6
2 -26.7 13.9
3 -26.7 13.3
4 -26.1 10.6
5 -15 12.8
6 -26.6 18.9
7 -30.6 21.7
8 -29.4 19.4
9 -27.8 17.8
感谢您的帮助, 我
最佳答案
问题是您没有正确指定 fill_between 的 x 参数。您传递的 range(len(...))
从 0 开始,而 Day_of_Year 从 1 开始,因此偏移量为 1。
[另外,我认为您的示例并不完整,因为您应该说您将 Day_of_Year
设置为数据索引。]
要解决您的问题,请将 max_hist_temp
的索引作为 fill_ Between
函数中的 x 参数传递:
plt.gca().fill_between(max_hist_temps.index, # CHANGED HERE
min_hist_temps, max_hist_temps,
facecolor='blue',
alpha = 0.25)
关于python - Matplotlib .fill_ Between() 偏移量-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44199833/