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:
x and y must have same first dimension, but have shapes (21453,) and (9,)
Any suggestions on how to fix this?
Advertisement
Answer
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)