繁体   English   中英

在 Python 中绘制高于阈值的值

[英]Plotting values above a threshold in Python

使用 pandas dataframe 绘制高于设定阈值的值时出现问题。

我有一个 dataframe,它有 21453 行和 20 列,其中一列只有 1 和 0 值。 我正在尝试使用以下代码 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)

但是得到以下错误:

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

对于如何解决这个问题,有任何的建议吗?

该错误可能是这一行plt.plot(df_smooth['Time'], lst1)的结果。 虽然 lst1 是df_smooth[Time]的子集,但df_smooth['Time']是完整系列。

我会做的解决方案是还构建一个过滤的 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])

另一种选择是建立一个子数据框 -

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

(假设 Y 列的正确列是Time ,否则只需将其替换为正确的列)

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

在尝试 plot 之前,您需要确保两个数据系列具有相同的长度。 一种方法是创建一个新列表,其中包含与 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)

我认为这应该有效。

暂无
暂无

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

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