Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plotting values above a threshold in Python

Having issues with plotting values above a set threshold using a pandas dataframe.

I have a dataframe that has 21453 rows and 20 columns, and one of the columns is just 1 and 0 values. I’m trying to plot this column using the following code:

lst1 = []
for x in range(0, len(df)): 
    if(df_smooth['Active'][x] == 1):
        lst1.append(df_smooth['Time'][x])
        
plt.plot(df_smooth['Time'], df_smooth['CH1'])
plt.plot(df_smooth['Time'], lst1)

But get the following errors:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x and y must have same first dimension, but have shapes (21453,) and (9,)

Any suggestions on how to fix this?

>Solution :

The error is probably the result of this line plt.plot(df_smooth['Time'], lst1). While lst1 is a subset of df_smooth[Time], df_smooth['Time'] is the full series.

The solution I would do is to also build a filtered x version for example –

lst_X = []
lst_Y = []
for x in range(0, len(df)): 
    if(df_smooth['Active'][x] == 1):
        lst_X.append(df_smooth['Time'][x])
        lst_Y.append(df_smooth['Time'][x])

Another option is to build a sub-dataframe –

sub_df = df_smooth[df_smooth['Active']==1]
plt.plot(sub_df['Time'], sub_df['Time'])

(assuming the correct column as Y column is Time, otherwise just replace it with the correct column)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading