[英]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.