Sergio Espejo Sergio Espejo - 3 months ago 16
Python Question

How tdo you run an elif function in a graph?

I have a dataset which has corresponding temperature values, with a column called CO2-rh:

import pandas as pd
df=pd.read_csv('F:/data32.csv',parse_dates=['Date'])
print (df)
Temperature unit unit.1 CO2 flux.1 %Root Resp CO2-Rh
4.5 umol/m2/s mg/cm^2/h 0.001210 26.5 0.000889
4.5 umol/m2/s mg/cm^2/h 0.001339 26.5 0.000984
6.5 umol/m2/s mg/cm^2/h 0.001339 26.5 0.000984
5.3 umol/m2/s mg/cm^2/h 0.001469 26.5 0.001080
4.0 umol/m2/s mg/cm^2/h 0.001598 26.5 0.001175
5.5 umol/m2/s mg/cm^2/h 0.001598 26.5 0.001175
5.0 umol/m2/s mg/cm^2/h 0.001771 26.5 0.001302
5.0 umol/m2/s mg/cm^2/h 0.001944 26.5 0.001429
4.5 umol/m2/s mg/cm^2/h 0.003110 26.5 0.002286
10.3 umol/m2/s mg/cm^2/h 0.001166 26.5 0.000857
9.0 umol/m2/s mg/cm^2/h 0.002030 26.5 0.001492


I have a dataset which has corresponding temperature values, with a column called CO2-rh. I want to tell my function, to divide my data set according to mean temperature, If the temperature is above the value 8.21, I want the data to go to dataset “a”, while anything that is equal to or below 8.21, go into data set” b” (i feel like this is the best way to plot two separate graphs). What can I do?
So far this is what I got.

if df['Temperature']> 8.212312312312307:
plt.plot(df['Temperature'],df['CO2-rh'],linewidth=3)
plt.show()

Answer

It looks like need boolean indexing with DataFrame.plot:

import matplotlib.pyplot as plt

mask = df['Temperature']> 8.212312312312307

df1 = df[mask]
df2 = df[~mask]
print (df1)
    Temperature       unit     unit.1  CO2 flux.1  %Root Resp    CO2-Rh
9          10.3  umol/m2/s  mg/cm^2/h    0.001166        26.5  0.000857
10          9.0  umol/m2/s  mg/cm^2/h    0.002030        26.5  0.001492

print (df2)
   Temperature       unit     unit.1  CO2 flux.1  %Root Resp    CO2-Rh
0          4.5  umol/m2/s  mg/cm^2/h    0.001210        26.5  0.000889
1          4.5  umol/m2/s  mg/cm^2/h    0.001339        26.5  0.000984
2          6.5  umol/m2/s  mg/cm^2/h    0.001339        26.5  0.000984
3          5.3  umol/m2/s  mg/cm^2/h    0.001469        26.5  0.001080
4          4.0  umol/m2/s  mg/cm^2/h    0.001598        26.5  0.001175
5          5.5  umol/m2/s  mg/cm^2/h    0.001598        26.5  0.001175
6          5.0  umol/m2/s  mg/cm^2/h    0.001771        26.5  0.001302
7          5.0  umol/m2/s  mg/cm^2/h    0.001944        26.5  0.001429
8          4.5  umol/m2/s  mg/cm^2/h    0.003110        26.5  0.002286


df1[['Temperature','CO2-Rh']].plot(linewidth=3)
df2[['Temperature','CO2-Rh']].plot(linewidth=3)
plt.show()