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'