Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
reusing the help tool axes
  • Loading branch information
fariza committed Apr 12, 2018
commit b89a904d2e12862a6e8cad8e18fbb2ee261402de
10 changes: 7 additions & 3 deletions lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,12 @@ def __init__(self, *args):
def enable(self, *args):
# Using custom axes label to prevent reuse of old axes
# https://github.com/matplotlib/matplotlib/issues/9024
self.text_axes = self.figure.add_axes((0, 0, 1, 1),
label='help_tool_axes')
if not self.text_axes:
self.text_axes = self.figure.add_axes((0, 0, 1, 1),
label='help_tool_axes')
self.text_axes.set_visible(True)
self.text_axes.clear()

table = Table(self.text_axes, bbox=[0, 0, 1, 1])
table.edges = 'B'
self.text_axes.add_table(table)
Expand Down Expand Up @@ -1063,7 +1067,7 @@ def _find_chars_in_width(self, fontsize):
return figwidth / charwidth

def disable(self, *args):
self.text_axes.remove()
self.text_axes.set_visible(False)
self.figure.canvas.draw_idle()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that after having pressed that button 10 times, you have 10 unused Axes in memory? Should it maybe rather reuse an existing axes, or comletely delete the unused axes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when creating the axes we specify the label, so if I understand it correctly it should reuse it if available

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That behavior (of reusing axes) is deprecated.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But's it's not available, because you removed it from the figure. And if you didn't remove it, it would cause a warning, telling you that creating an instance with the exact same arguments will return a new instance in the future.

Simple test case:

fig = plt.figure()
ax1 = fig.add_axes((0, 0, 1, 1), label='help_tool_axes')
ax1.remove()
ax2 = fig.add_axes((0, 0, 1, 1), label='help_tool_axes')
print(ax1==ax2)    # This will print False.

I think it would indeed be a good idea to reuse the axes, but I currently would not know how to do that. I.e. how would one add an axes to a figure which has previously been removed? (fig.add_axes(ax) does not seem to work.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create it once, call set_visible(True), call set_visible(False).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, now it creates it once, set the visibility to change.
I clear the axes before filling it because the tools are dynamic, so the content might change between different calls

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the visibility lets the axes stay in fig.axes. This might change any program's complete logic depending on whether or not you are using the ToolManager to show the figure and whether or not you have previously toggled the help or not.

Simple example:

for ax in fig.axes:
    # do something.


def _get_content(self, w0, w1, w2):
Expand Down