Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a62e8be

Browse files
committed
In ginput(), don't call show() if we can't.
`ginput` works even for figures that are not created by `plt.figure()` (and friends -- typically, that means we're embedding matplotlib in a GUI app)... except that `ginput` tries to call `fig.show()` to raise the figure, which would fail. Instead, only call `fig.show` if the figure has a manager (i.e., was created by `plt.figure`). Example (click once to start point selection, then make further clicks as in a normal `ginput` call): ``` import matplotlib; matplotlib.use("qt5agg") from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg from matplotlib.figure import Figure from PyQt5.QtCore import Qt from PyQt5.QtWidgets import * class Win(QMainWindow): def __init__(self): super().__init__() self.fig = fig = Figure() canvas = FigureCanvasQTAgg(fig) self.setCentralWidget(canvas) fig.add_subplot(111) self._cid = fig.canvas.mpl_connect( "button_press_event", self._start_ginput) def _start_ginput(self, event): self.fig.canvas.mpl_disconnect(self._cid) # Workaround for now; patch makes this unnecessary. self.fig.show = lambda: None print(self.fig.ginput(-1, timeout=3)) app = QApplication([]) win = Win() win.show() app.exec_() ```
1 parent 9c2412a commit a62e8be

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

lib/matplotlib/blocking_input.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ def __call__(self, n=1, timeout=30):
104104
self.events = []
105105
self.callbacks = []
106106

107-
# Ensure that the figure is shown
108-
self.fig.show()
107+
if hasattr(self.fig, "manager"):
108+
# Ensure that the figure is shown, if we are managing it.
109+
self.fig.show()
109110

110111
# connect the events to the on_event function call
111112
for n in self.eventslist:

0 commit comments

Comments
 (0)