0

I have this df:

          CODE      DATE    MONTH_DAY  PP       PP_INTERPOL
0       472606FA 2001-01-01   01-01    0.0         0.0
1       472606FA 2001-01-02   01-02    0.0         0.1
2       472606FA 2001-01-03   01-03    0.7         0.5
3       472606FA 2001-01-04   01-04    NaN         NaN
4       472606FA 2001-01-05   01-05    NaN         NaN
         ...        ...     ...    ...        ...
248220  47E2A75C 2021-04-26   04-26    0.3         0.3
248221  47E2A75C 2021-04-27   04-27    0.5         0.4
248222  47E2A75C 2021-04-28   04-28    0.5         0.5
248223  47E2A75C 2021-04-29   04-29    0.8         0.8
248224  47E2A75C 2021-04-30   04-30    NaN         NaN

I'm calculating some formulas for every code. So i did this code:

for code, data in df.groupby('CODE'):

  rmsef1=np.sqrt(np.nanmean((data['PP']-interpolated_array1)**2))
  rmsef2=np.sqrt(np.nanmean((data['PP']-interpolated_array2)**2))
  rmsef3=np.sqrt(np.nanmean((data['PP']-interpolated_array3)**2))
  rmsef4=np.sqrt(np.nanmean((data['PP']-interpolated_array4)**2))
  rmsef5=np.sqrt(np.nanmean((data['PP']-interpolated_array5)**2))
  .
  .
  .
  .
  rmsef20=np.sqrt(np.nanmean((data['PP']-interpolated_array20)**2)) 

  rmses=np.array([rmsef1,rmsef2,rmsef3,rmsef4,
       rmsef5,rmsef6,rmsef7,rmsef8,
       rmsef9,rmsef10,rmsef11,rmsef12,
       rmsef13,rmsef14,rmsef15,rmsef16,
       rmsef17,rmsef18,rmsef19,rmsef20])

So i'll get as many rmses variables as the number of codes (rmses is a array of 20 float values). I want to plot all of this rmses in a same graphic (linear plot), something similar to this image:

enter image description here

So i'm not sure how or where i should use the plt.plot. Or maybe i need to create another loop.

Would you mind to help me? Thanks in advance.

Javier
  • 493
  • 3
  • 15
  • Does this answer your question? [How to plot multiple functions on the same figure, in Matplotlib?](https://stackoverflow.com/questions/22276066/how-to-plot-multiple-functions-on-the-same-figure-in-matplotlib) – woblob Aug 06 '21 at 15:33
  • @woblob srry no. Cause i have more than rmses to plot. – Javier Aug 06 '21 at 16:04

1 Answers1

1

It is not necessary to add an additional loop. Inside your already existing for loop, add plt.plot immediately after computing the rmses values. Depending on how many codes you have in your dataframe you may want to add a legend to make your chart easier to read. You can do that by adding label=code as argument of plt.plot, and add plt.legend outside the loop.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('sample.csv')

for code, data in df.groupby('CODE'):

    rms_1_20 = [data.iloc[i]['PP'] for i in range(20)]
    rmses = rms_1_20  # just to explicitate the aquisition of 20 RMS values.

    plt.plot(range(len(rmses)), rmses, label=code)

plt.legend(loc="upper right")
plt.show()

plt_rmses

n1colas.m
  • 3,863
  • 4
  • 15
  • 28