简体   繁体   English

在 Python 中绘制高于阈值的值

[英]Plotting values above a threshold in Python

Having issues with plotting values above a set threshold using a pandas dataframe.使用 pandas dataframe 绘制高于设定阈值的值时出现问题。

I have a dataframe that has 21453 rows and 20 columns, and one of the columns is just 1 and 0 values.我有一个 dataframe,它有 21453 行和 20 列,其中一列只有 1 和 0 值。 I'm trying to plot this column using the following code:我正在尝试使用以下代码 plot 此列:

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) .该错误可能是这一行plt.plot(df_smooth['Time'], lst1)的结果。 While lst1 is a subset of df_smooth[Time] , df_smooth['Time'] is the full series.虽然 lst1 是df_smooth[Time]的子集,但df_smooth['Time']是完整系列。

The solution I would do is to also build a filtered x version for example -我会做的解决方案是还构建一个过滤的 x 版本,例如 -

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) (假设 Y 列的正确列是Time ,否则只需将其替换为正确的列)

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.似乎您正在尝试使用 plt.plot() function plot 两个不同的数据系列,这是导致错误的原因,因为 plt.plot() 期望两个系列具有相同的长度。

You will need to ensure that both data series have the same length before trying to plot them.在尝试 plot 之前,您需要确保两个数据系列具有相同的长度。 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.一种方法是创建一个新列表,其中包含与 df_smooth['Time'] 数据系列相同数量的元素,然后用 lst1 数据系列中的相应值填充它。

# 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.我认为这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python 中更改高于阈值的矩阵值 - Changing the values of a matrix above a threshold in python 在Python中绘制高于阈值的值 - Plotting occurrences for values higher than a threshold in Python 删除高于阈值的值 - Remove values above threshold 使用 Python 对图像中高于阈值的像素值求和的最快方法 - Fastest way to sum the values of the pixels above a threshold in an image with Python Python-在数组中查找序列:当两个值低于阈值时开始,当两个值高于阈值时结束 - Python - Find sequence in array: start when two values below threshold, end when two values above threshold 基于高于该阈值的值的平均值为变量设置阈值 - Threshold a variable based on the average of values above that threshold 如何在没有if语句的情况下在python中设置阈值(如果低于阈值则为零,如果高于阈值则为零) - How to threshold values in python without if statement (to zero if below threshold, same if above) 从python中的某个阈值绘图 - Plotting from a certain threshold in python Python绘制错误条,在该点之上和之下具有不同的值 - Python plotting error bars with different values above and below the point 删除缺失值高于阈值熊猫的列 - Remove Columns with missing values above a threshold pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM