I am currently working with a dataset that includes signals with a duration of around 10 seconds and a sampling time of 0.1 seconds. My goal is to extract a specific part of this data and save it into a python dictionary. The relevant part is about 4 seconds long.
Ideally this is what I would like to do:
Plot the whole 10 seconds of data.
Mark the relevant part of the signals for example with a bounding box.
Extract the data within the bounding box after closing the plot window or pushing a button.
Go back to 1. and take new data.
I saw that matplotlib has the ability to draw patches and extract the data points within the patch. Is it maybe possible to add a patch to the plot after the plot was created (after the plt.show() command was executed)?
Thank you in advance and best regards,
Manuel
text() is the main function we can use to add text to a plot. We start by adding the string “function” with fontsize=12 to the location (10, 20). By default, the location is given in data coordinates. The coordinate system can be changed using the transform parameter.
To fill the area under the curve, put x and y with ste="pre", using fill_between() method. Plot (x, y1) and (x, y2) lines using plot() method with drawstyle="steps" method. To display the figure, use show() method.
You may use a SpanSelector
.
You basically only need to add a line for saving to the matplotlib example.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(211)
x = np.arange(0.0, 5.0, 0.01)
y = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))
ax.plot(x, y, '-')
ax.set_ylim(-2, 2)
ax.set_title('Press left mouse button and drag to test')
ax2 = fig.add_subplot(212)
line2, = ax2.plot(x, y, '-')
def onselect(xmin, xmax):
indmin, indmax = np.searchsorted(x, (xmin, xmax))
indmax = min(len(x) - 1, indmax)
thisx = x[indmin:indmax]
thisy = y[indmin:indmax]
line2.set_data(thisx, thisy)
ax2.set_xlim(thisx[0], thisx[-1])
ax2.set_ylim(thisy.min(), thisy.max())
fig.canvas.draw_idle()
# save
np.savetxt("text.out", np.c_[thisx, thisy])
# set useblit True on gtkagg for enhanced performance
span = SpanSelector(ax, onselect, 'horizontal', useblit=True,
rectprops=dict(alpha=0.5, facecolor='red'))
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With