Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select area within a plot (Python) and extract the data within the area

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:

  1. Plot the whole 10 seconds of data.

  2. Mark the relevant part of the signals for example with a bounding box.

  3. Extract the data within the bounding box after closing the plot window or pushing a button.

  4. 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

like image 309
M. Schmidt Avatar asked Jul 08 '17 21:07

M. Schmidt


People also ask

Which command is used to print a text at a specified location on the plot area in Python?

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.

How do you plot area under a curve in Python?

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.


1 Answers

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()

enter image description here

like image 196
ImportanceOfBeingErnest Avatar answered Sep 27 '22 15:09

ImportanceOfBeingErnest