简体   繁体   中英

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:

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

Any suggestions on how to fix this?

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)

It seems like you are trying to plot two different data series using the plt.plot() function, this is causing the error because plt.plot() expects both series to have the same length.

You will need to ensure that both data series have the same length before trying to plot them. One way to do this is to create a new list that contains the same number of elements as the df_smooth['Time'] data series, and then fill it with the corresponding values from the lst1 data series.

# Create a new list with the same length as the 'Time' data series
lst2 = [0] * len(df_smooth['Time'])

# Loop through the 'lst1' data series and copy the values to the corresponding
# indices in the 'lst2' data series
for x in range(0, len(lst1)):
    lst2[x] = lst1[x]

# Plot the 'Time' and 'lst2' data series using the plt.plot() function
plt.plot(df_smooth['Time'], df_smooth['CH1'])
plt.plot(df_smooth['Time'], lst2)

I think this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM