0

I have a pandas dataframe that I'm trying to plot as a line graph. I have a column that indicates true or false based on the time period, and all I'm looking to do is to have my line graph shaded by different colors based on that value.

This is what my dataframe looks like:

   date        value1        value2        value3      yesno  
 1/1/2017    4.092913986   0.983422333   4.702443788        1  
 1/2/2017    1.297297396   0.944562353   93.28873738        1  
 1/3/2017    9.340254094    0.29748964    63.0201697        0  
 1/4/2017    1.785673265   0.266832844   15.82940576        0  
 1/5/2017    1.795707079   0.059327578   76.17126027        0  
 1/6/2017    6.973433264   0.606546753   92.23754049        0  
 1/7/2017    4.720144158   0.007475406   90.90629644        0  
 1/8/2017    6.654395447   0.345946112   55.06564897        1  
 1/9/2017    5.066128977   0.636196266   79.01661793        1  
 1/10/2017   5.036420596    0.71724815   99.56291692        0  
 1/11/2017   9.056591185   0.413846139   62.96214739        1  
 1/12/2017   2.351980932   0.021501056   53.48078161        0  
 1/13/2017   4.010542244   0.381479612   78.55923149        0  
 1/14/2017   0.175470941   0.152152033   35.02354055        0  
 1/15/2017   5.702023064   0.717303458   85.70226712        1  
 1/16/2017   2.970976482   0.816395807   37.39305001        1  
 1/17/2017   0.186432777   0.218164339   44.57253955        0  

I know there's the axvspan function, but how do I apply it to this? thanks for your help!

x= useperftable['date']
y= useperftable[['value1','value2','value3']]
bo = useperftable['yesno']
fig, ax = plt.subplots()
ax.fill_between(x, 0, 1, where=bo, alpha=0.4, transform=ax.get_xaxis_transform())
plt.plot(x,y)
plt.show()
Dick Thompson
  • 599
  • 1
  • 12
  • 26
  • You want the line graph to be colorized or the background of the line graph? – ImportanceOfBeingErnest Nov 16 '17 at 21:30
  • the background of the line graph, similar to this https://stackoverflow.com/questions/23248435/fill-between-two-vertical-lines-in-matplotlib but if possible a color for 0 and a color for 1 – Dick Thompson Nov 16 '17 at 21:31
  • [This question](https://stackoverflow.com/questions/43233552/how-do-i-use-axvfill-with-a-boolean-series) is what you are looking for. You would need two such fill_between functions with each of a different color. – ImportanceOfBeingErnest Nov 16 '17 at 21:35
  • is that example using a dataframe? random values always confuse me – Dick Thompson Nov 16 '17 at 21:39
  • There is no difference between some values that you post in the question and random values, since for the reader of your question those values also seem pretty random. – ImportanceOfBeingErnest Nov 16 '17 at 21:44
  • how do I replace the 0,1 with my dataframe? that's where I get confused- and I'm not defining x in my matplotlib, I'm just inputting the columns for x and y – Dick Thompson Nov 16 '17 at 21:44
  • This is the typerror I get: TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' – Dick Thompson Nov 16 '17 at 21:47
  • May I remind you that your question does not contain a single line of code?! So what exactly are you talking about? See [mcve]. – ImportanceOfBeingErnest Nov 16 '17 at 21:49
  • I included and modified my code based on your example, again I'm not sure where I'm going wrong and don't think it's the same example- that one uses random arrays and includes numpy arrays, not a pandas dataframe – Dick Thompson Nov 16 '17 at 21:52
  • A dataframe column is essentially a numpy array. You can get it explicitely as numpy array via `df["columnname"].values`. The remaining problem is covered by [this answer](https://stackoverflow.com/questions/29329725/pandas-and-matplotlib-fill-between-vs-datetime64). – ImportanceOfBeingErnest Nov 16 '17 at 22:04

0 Answers0