Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据Matplotlib中的条件标记特定点_Python_Pandas_Numpy_Dataframe_Matplotlib - Fatal编程技术网

Python 根据Matplotlib中的条件标记特定点

Python 根据Matplotlib中的条件标记特定点,python,pandas,numpy,dataframe,matplotlib,Python,Pandas,Numpy,Dataframe,Matplotlib,我绘制了df['Data']的最小点 Timestamp = pd.date_range('2020-02-06 08:23:04', periods=1000, freq='s') df = pd.DataFrame({'Timestamp': Timestamp, 'Data': 30+15*np.cos(np.linspace(0,10,Timestamp.size))}) df['timediff'] = (df['Timestamp'].shif

我绘制了
df['Data']
的最小点

Timestamp = pd.date_range('2020-02-06 08:23:04', periods=1000, freq='s')
df = pd.DataFrame({'Timestamp': Timestamp,
                   'Data': 30+15*np.cos(np.linspace(0,10,Timestamp.size))})

df['timediff'] = (df['Timestamp'].shift(-1) - df['Timestamp']).dt.total_seconds()   
df['datadiff'] = df['Data'].shift(-1) - df['Data']
df['gradient'] = df['datadiff'] / df['timediff']

min_pt = np.min(df['Data'])       
# filter_pt = df.loc(df['gradient'] >= -0.1) # & df.loc[i, 'gradient'] <=0.1

mask = np.array(df['Data']) == min_pt 
color = np.where(mask, 'blue', 'yellow')

fig,ax = plt.subplots(figsize=(20,10))
# plt.plot_date(df['Timestamp'], df['Data'], '-' )
ax.scatter(df['Timestamp'], df['Data'], color=color, s=10)
plt.ticklabel_format
plt.show()
掩码之前
返回错误的依据:

TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]
我觉得我做的不是很有效率。如何更有效地做到这一点


更新:

带代码

Timestamp = pd.date_range('2020-02-06 08:23:04', periods=1000, freq='s')
df = pd.DataFrame({'Timestamp': Timestamp,
                   'Data': 30+15*np.cos(np.linspace(0,10,Timestamp.size))})

df['timediff'] = (df['Timestamp'].shift(-1) - df['Timestamp']).dt.total_seconds()    
df['datadiff'] = df['Data'].shift(-1) - df['Data']
df['gradient'] = df['datadiff'] / df['timediff']

fig,ax = plt.subplots(figsize=(20,10))
df1 = df[(df.gradient <= 0.1) & (df.gradient >= -0.1)]
plt.plot(df1.Timestamp,df1.Data, label="filter")
plt.show()
Timestamp=pd.date\u范围('2020-02-06 08:23:04',句点=1000,频率=s')
df=pd.DataFrame({'Timestamp':时间戳,
“数据”:30+15*np.cos(np.linspace(0,10,Timestamp.size))}
df['timediff']=(df['Timestamp'].shift(-1)-df['Timestamp']).dt.total_seconds()
df['datadiff']=df['Data'].shift(-1)-df['Data']
df['gradient']=df['datadiff']/df['timediff']
图,ax=plt.子批次(图尺寸=(20,10))
df1=df[(df.gradient=-0.1)]
plt.plot(df1.Timestamp,df1.Data,label=“filter”)
plt.show()
它回来了

将范围更改为

df1 = df[(df.gradient <= 0.01) & (df.gradient >= -0.01)]
df1=df[(df.gradient=-0.01)]
它回来了


为什么?

在每个条件上添加括号,这样您就可以逐行执行逻辑和

df1 = df[(df.gradient <= 0.1) & (df.gradient >= -0.1)]
这将是最终图像:

编辑

如果只需要渐变位于范围内的第一个点,请创建组,然后使用groupby

df['groups'] = ((df.gradient > 0.1) | (df.gradient < -0.1)).cumsum()

df2 = df[(df.gradient <= 0.1) & (df.gradient >= -0.1)]
    .groupby('groups').agg({'Timestamp':'first', 'Data':'first'})

#        Timestamp              Data
# groups        
# 0      2020-02-06 08:23:04    45.000000
# 168    2020-02-06 08:27:05    18.814188
# 336    2020-02-06 08:32:19    41.201294
# 504    2020-02-06 08:37:33    18.783251
df['groups']=((df.gradient>0.1)|(df.gradient<-0.1)).cumsum()
df2=df[(df.gradient=-0.1)]
.groupby('groups').agg({'Timestamp':'first','Data':'first'})
#时间戳数据
#团体
# 0      2020-02-06 08:23:04    45.000000
# 168    2020-02-06 08:27:05    18.814188
# 336    2020-02-06 08:32:19    41.201294
# 504    2020-02-06 08:37:33    18.783251

谢谢jcaliz!是否有办法只标记每个范围内的第一个数据点?
plt.scatter(df1.Timestamp,df1.Data, label="filter")
df['groups'] = ((df.gradient > 0.1) | (df.gradient < -0.1)).cumsum()

df2 = df[(df.gradient <= 0.1) & (df.gradient >= -0.1)]
    .groupby('groups').agg({'Timestamp':'first', 'Data':'first'})

#        Timestamp              Data
# groups        
# 0      2020-02-06 08:23:04    45.000000
# 168    2020-02-06 08:27:05    18.814188
# 336    2020-02-06 08:32:19    41.201294
# 504    2020-02-06 08:37:33    18.783251