繁体   English   中英

Matplotlib .fill_between()偏移量-1

[英]Matplotlib .fill_between() Offset By -1

我是新手,并尝试.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设置为数据的索引。]

要解决您的问题,请在fill_between函数max_hist_temp索引作为x param fill_between

plt.gca().fill_between(max_hist_temps.index,   # CHANGED HERE
        min_hist_temps, max_hist_temps,
        facecolor='blue',
        alpha = 0.25)

您可能希望使用数据Day_of_Year列作为实际x值,而不是其他一些索引。

import io
import pandas as pd
import matplotlib.pyplot as plt

u = u"""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"""



df_combo = pd.read_csv(io.StringIO(u), delim_whitespace=True)
max_hist_temps = df_combo['hist_max']
min_hist_temps = df_combo['hist_min']
plt.figure()
plt.plot(df_combo['Day_of_Year'], max_hist_temps, '-.r',
         df_combo['Day_of_Year'], min_hist_temps, '-.b'
        )
plt.gca().fill_between(df_combo['Day_of_Year'],
        min_hist_temps, max_hist_temps,
        facecolor='blue',
        alpha = 0.25)
ax = plt.gca()
ax.axis([0,10,-45,45])
plt.show()

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM