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:
JavaScript
x
6
1
lst1 = []
2
for x in range(0, len(df)):
3
if(df_smooth['Active'][x] == 1):
4
lst1.append(df_smooth['Time'][x])
5
6
JavaScript
1
3
1
plt.plot(df_smooth['Time'], df_smooth['CH1'])
2
plt.plot(df_smooth['Time'], lst1)
3
But get the following errors:
JavaScript
1
2
1
x and y must have same first dimension, but have shapes (21453,) and (9,)
2
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 –
JavaScript
1
7
1
lst_X = []
2
lst_Y = []
3
for x in range(0, len(df)):
4
if(df_smooth['Active'][x] == 1):
5
lst_X.append(df_smooth['Time'][x])
6
lst_Y.append(df_smooth['Time'][x])
7
Another option is to build a sub-dataframe –
JavaScript
1
3
1
sub_df = df_smooth[df_smooth['Active']==1]
2
plt.plot(sub_df['Time'], sub_df['Time'])
3
(assuming the correct column as Y column is Time
, otherwise just replace it with the correct column)