如何在 Python 中使用日期时间和 scipy 峰值进行绘图? [英] How to make plot with datetime and scipy peaks in Python?

查看:53
本文介绍了如何在 Python 中使用日期时间和 scipy 峰值进行绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个问题中选取了一个示例,并将其调整到我的数据集,但是在制作绘图时,我被卡住了.我知道如何制作日期时间 + 值图,但我不知道如何进行组合.

我的回复如下::

<块引用>

返回:峰值: ndarray
x 中满足所有给定条件的峰的指数.

您的 peaks 变量是峰值的 indices 数组.因此,为了访问这些峰的x和y坐标,您应该替换:

plt.scatter(x, y, s=1,c='b',label='quality')plt.plot(peaks,my_dataset ['quality'] [peaks],"x")

使用

plt.scatter(x, y, s=1,c='b',label='quality')plt.plot(x[peaks], y[peaks], x")

I took one example from a question and I adapted it to my data set, but when it comes to making plots, I got stuck. I know how to make a date time + values plot, but I didn't figure out how can I make a combination.

The response I took is the following: Response.

The example has the following code:
Example Code:

import numpy as np                    
import matplotlib.pyplot as plt
from scipy.signal import find_peaks 

np.random.seed(42)

# borrowed from @Majid Mortazavi's answer
random_number1 = np.random.randint(0, 200, 20)
random_number2 = np.random.randint(0, 20, 100)
random_number = np.concatenate((random_number1, random_number2))
np.random.shuffle(random_number)

peaks, _ = find_peaks(random_number, height=100)

plt.plot(random_number)
plt.plot(peaks, random_number[peaks], "x")
plt.show()

I adapted it to my real-values dataset and I have the following code:
Adapted Code:

peaks, _ = find_peaks(my_dataset['quality'], height=500)

plt.figure(figsize=(14,12))
x = my_dataset.index
y = my_dataset.quality
plt.scatter(x, y, s=1,c='b',label='quality')
plt.plot(peaks, my_dataset['quality'][peaks], "x")

plt.xlabel('datetime')
plt.ylabel('values')
plt.legend(loc='best')
plt.title('Qualities')
plt.show()

Initially, my code for plotting was this and it worked:
Initial Code:

plt.figure(figsize=(14,12))
x = my_dataset.index
y = my_dataset.quality
plt.scatter(x, y, s=1,c='b',label='quality')

plt.xlabel('datetime')
plt.ylabel('values')
plt.legend(loc='best')
plt.title('Qualities')
plt.show()

But I need to have the same plot, but with the "X" sign on the computed peaks.

So the final result should be something like this: (I edited the initial plot in paint for a visual idea of the final result)

I honestly do not know how to make it work. Can you please help me?

I highly appreciate any effort to help me!!! Thank you so much in advanced!

解决方案

As documentation of scipy.signal.find_peaks reports:

Returns: peaks: ndarray
Indices of peaks in x that satisfy all given conditions.

Your peaks variable is an array of the indices of peaks. So, in order to access x and y coordinates of these peaks, you should replace:

plt.scatter(x, y, s=1,c='b',label='quality')
plt.plot(peaks, my_dataset['quality'][peaks], "x")

with

plt.scatter(x, y, s=1,c='b',label='quality')
plt.plot(x[peaks], y[peaks], "x")

这篇关于如何在 Python 中使用日期时间和 scipy 峰值进行绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆