尽管使用where参数,为什么fill_between仍填充相同的颜色?

时间:2019-03-28 06:01:40

标签: python matplotlib

我的代码很简单。问题描述/我想做的事情:我有两个列表-rmormw。我正在尝试获得它们之间的折线图。效果很好。第一个fill_between也有效(... where rmo<rmw...facecolor='green')。但是,fill_between(..where rmo>rmw..facecolor='pink')不起作用。我已经检查过,并且rmo的确确实大于rmw的值。没有错误,但是整个图变成绿色填充,没有任何部分是粉红色。 这是完整的代码:

import pandas as pd
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

#Reading the excel files

df17 = pd.read_excel(open(r'C:\Users\ADMIN\Desktop\Abhishek\Excel 
Files\Team Stats Wolfsburg 17-18.xlsx','rb'))
df18 = pd.read_excel(open(r'C:\Users\ADMIN\Desktop\Abhishek\Excel 
Files\Team Stats Wolfsburg 18-19.xlsx','rb'))

c=0
d=0
wolf1=[]
opponents1=[]
wolf2=[]
opponents2=[]
rmw=[]
rmo=[]
##Getting a list of the values I need
for i in df17["Team"]:
    if i=="Wolfsburg":
        wolf1.append(df17.iloc[c,7])
    else:
        opponents1.append(df17.iloc[c,7])
    c=c+1
#Reversing the list to make sure its chronological
wolf1.reverse()
opponents1.reverse()

##Same method to get values from the other excel file
for j in df18["Team"]:
    if j=="Wolfsburg":
        wolf2.append(df18.iloc[d,7])
    else:
        opponents2.append(df18.iloc[d,7])
    d=d+1
wolf2.reverse()
opponents2.reverse()

##Concatenating 
totwol=wolf1+wolf2
totopp=opponents1+opponents2

##Removing nan values
totopp = [tot for tot in totopp if str(tot) != 'nan']

##X-axis
xaxis=np.linspace(1,76,76, endpoint=True)

##Calculating rolling averages
for k in range(0,76):
        mean= 
((totwol[k]+totwol[k+1]+totwol[k+2]+totwol[k+3]+totwol[k+4])/5)
        rmw.append(mean)
for z in range(0,76):
        mean2= 
((totopp[z]+totopp[z+1]+totopp[z+2]+totopp[z+3]+totopp[z+4])/5)
        rmo.append(mean2)    

##Plotting
ax.plot(xaxis,rmw,label="xG Created")
ax.plot(xaxis,rmo, color="red",label="xG Conceded")
ax.plot([47,47],[0.5,2.6],label="2018-19 begins",color="g")
ax.plot([1,1],[0.5,2.6],label="2017-18 begins",color="yellow")

##Problem area
ax.fill_between(xaxis, rmw, rmo, where=rmw <= rmo, facecolor='g', 
interpolate=True)
ax.fill_between(xaxis, rmw, rmo, where=rmw > rmo, facecolor='pink', 
interpolate=True)

plt.show()

以下是rmo,rmw的示例值:rmo=[1.7,1.6,2.0,2.4],rmw=[1.4,1.6,1.6,1.1]

As you can see everything is green

我在做什么错了?

0 个答案:

没有答案