Home > Enterprise >  How to plot line graphs with a specific color if a condition is true?
How to plot line graphs with a specific color if a condition is true?

Time:05-23

I have this df:

              MONTH   PP  CORRELATION
1632        January  15.4  0.923077
1633      Febrruary  58.6  0.923077
1634        March    54.6  0.923077
1635        April    9.6   0.923077
1636        May      13.4  0.923077
          etc...   etc...    etc...
42383      January   0.5   0.874126
42384    February    0.1   0.874126
42385      March     0.2   0.874126
42386      April     0.7   0.874126
42387      May       13.0  0.874126
           etc...   etc...       etc...

[312 rows x 3 columns]

I want to plot df['PP'] and df['MONTH'] data every 12 rows, with green line if the df['CORRELATION'] >= 0.95 and red line if doesn't meet the condition. Each line graph must have 12 df['PP'] values. The df['CORRELATION'] values for each 12 df['PP'] values are the same.

So i tried this code:

for i in df.iterrows():
    if float(df.iloc[12*i:12*(i 1)]['CORR'])>=0.95:
        plt.plot(df.iloc[12*i:12*(i 1)]['MES'],df.iloc[12*i:12*(i 1)]['PP'], color='green',
             alpha=0.6)
    else:
        plt.plot(df.iloc[12*i:12*(i 1)]['MES'],df.iloc[12*i:12*(i 1)]['PP'], color='red',
             alpha=0.6)

But i got this error:

TypeError: can only concatenate tuple (not "int") to tuple

I don't understand why cause im using the float() function.

Would you mind to help me?

Thanks in advance.

CodePudding user response:

df.iterrows() does not return row numbers. It returns a tuple for each row which has the index value and a Series that is the actual row data. It's your [12*i:12*(i 1)] that is causing the error, because i is a tuple.

I'm not sure what you were going after with that, so I don't know quite what to suggest in its stead.

CodePudding user response:

df.iterrows() returns tuple object, this object's secod element is series object, that why you must use series object index's. if you add **'PP'** index after your **i** in for loop most probably your problem will solve

  • Related