Commit a62e8be
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
1 file changed
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
108 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
| |||
0 commit comments