简体   繁体   English

Matplotlib .fill_between()偏移量-1

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

I'm a newbie and trying to .fill_between() two lines on my chart. 我是新手,并尝试.fill_between()在我的图表上的两行。 I've tried everything I can think of to ensure the underlying data is correct, but every time I chart this the .fill_between() appears one value on the x-axis short than what it should be. 我已经尝试了所有我能想到的以确保底层数据是正确的,但每次我绘制图表时, .fill_between()在x轴上显示的一个值比它应该的短。 Here's my code: 这是我的代码:

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()

And here's some sample data: 以下是一些示例数据:

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

Thank you for your help, Me 谢谢你的帮助,我

The problem is that you are not specifying properly the x parameter of fill_between . 问题是你没有正确指定fill_between的x参数。 You are passing range(len(...)) which starts from 0, while your Day_of_Year starts from one, hence the 1-offset. 你传递的range(len(...))从0开始,而你的Day_of_Year从1开始,因此是1偏移。

[Plus, I think your example is not complete as you should say that you set Day_of_Year as index of your data.] [另外,我认为您的示例并不完整,因为您应该说您将Day_of_Year设置为数据的索引。]

To fix your problem, pass the index of max_hist_temp as x param in the fill_between function: 要解决您的问题,请在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)

You may want to use the Day_of_Year column of the dataframe as the actual x values, instead of some other index. 您可能希望使用数据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