Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

SpanSelector does not work when used in method #14276

Copy link
Copy link
Closed
@ChristianF88

Description

@ChristianF88
Issue body actions

Hi there,

I don't know how to get the SpanSelector working when the plot is created within a class definition. The plot is created but loses it's interactive capabilities. To illustrate this issue I simply put a standard example into a class definition. Has anybody any ideas?

Thanks in advance!!

Working example:

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

# Fixing random state for reproducibility
np.random.seed(19680801)

fig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6))
ax1.set(facecolor='#FFFFCC')

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_ylim(-2, 2)
ax1.set_title('Press left mouse button and drag to test')

ax2.set(facecolor='#FFFFCC')
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()

# Set useblit=True on most backends for enhanced performance.
span = SpanSelector(ax1, onselect, 'horizontal', useblit=True,
                    rectprops=dict(alpha=0.5, facecolor='red'))


plt.show()

My code:

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


# Fixing random state for reproducibility
np.random.seed(19680801)
class SpanTest():
    def plot(self):
        fig, (ax1, ax2) = plt.subplots(2, figsize=(8, 6))
        ax1.set(facecolor='#FFFFCC')

        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_ylim(-2, 2)
        ax1.set_title('Press left mouse button and drag to test')

        ax2.set(facecolor='#FFFFCC')
        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()

        # Set useblit=True on most backends for enhanced performance.
        span = SpanSelector(ax1, onselect, 'horizontal', useblit=True,
                            rectprops=dict(alpha=0.5, facecolor='red'))


        plt.show()
        
st = SpanTest()
st.plot()

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 3.0.3
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.6
  • Jupyter version (if applicable):
  • Other libraries: numpy-version -> 1.16.3

I installed matplotlib via pip. I'm not using conda.

Thank you very much!
Have a great one!
Christian

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.