python - Matplotlib .fill_ Between() 偏移量-1

标签 python python-3.x pandas matplotlib

我是新手,正在尝试在图表上.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/

相关文章:

python - 如何拼接一个列表并使用拼接部分创建一个新列表?

android - Python 3.5 - Kivy Android - ScrollableLabel(ScrollView) - 两天处理一行代码

python - 删除python中的大量空格

python - 一个 View 中的不同查询集

python - 在python中解析双管道分隔文件

python - 循环中从数据帧获取最大值和最小值

python - 从计数表计算相对频率

python - 将 pandas 组保存到单独的 CSV 文件

python - 是否可以在 Excel 中使用 Python 动态更改公式引用的路径?

python - 将分类列添加到预测模型中