#### Combine Rectangle Selector and Span selector

I wanted to combine the rectangle and span selector.
The idea behind it is that I have 2 plots.
The first plot shows the whole measurement series.
In this diagram I wanted to select an interesting area with the rectangle selector.
In the second plot the selected area from the first plot should be shown/zoomed in more detail.
Unfortunately I do not have much experience in programming, so your help is very welcome.

To make it a little clearer:
The span selector is nearly what i need.
But instead of using a span i would like to use a rectangle.
Additional it is possible that the y-axis always goes from the bottom the to the top?
Therefore i only have to move the rectangle selector to the right position on the x-axis?

Span Selector:

``````import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector

fig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6))

# x = Simulation['position of pig [m]']
# y = Simulation['velocity [m/s]']

x = np.arange(0.0, 5.0, 0.01)
y = np.sin(2*np.pi*x) + 0.5*np.random.randn(len(x))

ax1.plot(x, y, '-')

# ax1.set_title('Press left mouse button and drag to test')

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, thisx[-1])
ax2.set_ylim(thisy.min(), thisy.max())
fig.canvas.draw()

span = SpanSelector(ax1, onselect, 'horizontal', useblit=True,
rectprops=dict(alpha=0.5, facecolor='red'))

plt.show()
``````

Rectangle Selector:

``````from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt

def line_select_callback(eclick, erelease):
'eclick and erelease are the press and release events'
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
print(" The button you used were: %s %s" % (eclick.button, erelease.button))

def toggle_selector(event):
print(' Key pressed.')
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
print(' RectangleSelector deactivated.')
toggle_selector.RS.set_active(False)
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
print(' RectangleSelector activated.')
toggle_selector.RS.set_active(True)

fig, current_ax = plt.subplots()                 # make a new plotting range
N = 100000                                       # If N is large one can see
x = np.linspace(0.0, 10.0, N)                    # improvement by use blitting!

plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7)  # plot something
plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)

print("n      click  -->  release")

# drawtype is 'box' or 'line' or 'none'
toggle_selector.RS = RectangleSelector(current_ax, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3],  # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
plt.connect('key_press_event', toggle_selector)
plt.show()
``````

Thank you and have a nice day

Source: Python Questions