1

Suppose data is a one-dimensional numpy.ndarray, and consider the following code written in an interactive console session, for instance with iPython:

fig, ax = plt.subplots()
p = ax.plot(data)
ax.set_xlabel('the x label')
fig.show()

Suppose I realize the xlabel is too small, and I want to change the font size, or that I've forgotten to set a ylabel, or add a legend. If I close the plot window, I have to go jump through hoops to create a dummy manager to recover the work I've already done on the axes and figure.

What is the simplest way (requiring the fewest commands) to amend my mistake and make changes to the axes and figure without having to essentially start from scratch?

There are many posts that give related answers, but most of them involve explaining what happens when the figure is closed. My question is: how do I leverage the object-oriented interface for Matplotlib to avoid re-doing work I've already done? I have references to p, ax and fig. Surely there is some way of re-utilizing them.

EDIT:

Replying to a comment, I get the following AttributeError when trying to simply invoke fig.show() again after closing it. The issue is that manager.show() fails, which seems in agreement with the post I linked.

AttributeError                            Traceback (most recent call last)
<ipython-input-36-3b825cbc0337> in <module>
----> 1 fig1.show()

/gdn/centos7/0001/x3/prefixes/desres-python/3.7.2-04c7__f61afbbaadc6/lib/python3.7/site-packages/matplotlib/figure.py in show(self, warn)
    435         if manager is not None:
    436             try:
--> 437                 manager.show()
    438                 return
    439             except NonGuiException:

/gdn/centos7/0001/x3/prefixes/desres-python/3.7.2-04c7__f61afbbaadc6/lib/python3.7/site-packages/matplotlib/backends/_backend_tk.py in show(self)
    566                 self.canvas.draw_idle()
    567             # Raise the new window.
--> 568             self.canvas.manager.window.attributes('-topmost', 1)
    569             self.canvas.manager.window.attributes('-topmost', 0)
    570             self._shown = True

AttributeError: 'NoneType' object has no attribute 'attributes'
snar
  • 145
  • 5
  • In IPython you just call `fig.show()` again to see the figure again. – ImportanceOfBeingErnest Nov 25 '19 at 18:25
  • This does not work for me (matplotlib version 3.0.2, ipython 7.2.0) and iPython throws an AttributeError. See the post I linked. Indeed, if it were the case that this worked seamlessly, I would not imagine there would be so many posts about it. – snar Nov 25 '19 at 18:39
  • Those posts are not about IPython. What you observe seems like a bug in the tkagg backend. Can you use the `Qt5Agg` backend instead (`import matplotlib; matplotlib.use("Qt5Agg")`)? – ImportanceOfBeingErnest Nov 25 '19 at 18:44
  • I see. When I set `matplotlib.use('Qt5Agg')` and hit `fig.show()`, a Figure window pops up, but the plot does not display and the window simply hangs until I force kill it. – snar Nov 25 '19 at 18:51
  • Does [this](https://stackoverflow.com/questions/31729948/matplotlib-how-to-show-a-figure-that-has-been-closed) help? – learner Nov 25 '19 at 18:55
  • Did you close and restart the IPython session before setting the backend? – ImportanceOfBeingErnest Nov 25 '19 at 18:58
  • learner: That is the same post I linked to in my opening post. ImportanceOfBeingErnest: I did, but you've helpfully pointed out this is an iPython issue and not a matplotlib issue, since I've been able to get the functionality I want by just running the Python3 interpreter from shell. That helps a lot, thanks. – snar Nov 25 '19 at 19:06

0 Answers0