我是新手,并试图在我的图表上.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
感谢您的帮助, 我
答案 0 :(得分:4)
问题是您没有正确指定fill_between的x参数。您正在传递从{0开始的range(len(...))
,而您的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)
答案 1 :(得分:1)
您可能希望将数据框的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()