v3.3.0 breaks sca(ax) · Issue #19380 · matplotlib/matplotlib · GitHub
Skip to content

v3.3.0 breaks sca(ax) #19380

Description

@david-c-wong

Hi,

I encountered an error calling sca(ax) together with usage of FigureCanvasTkAgg(). Following would be a simple sequence illustrating the problem.

 // Note that plt.gca().figure == figure
... = FigureCanvasTkAgg(figure, ...)
plt.sca(plt.gca())

The problem is because FigureCanvasTkAgg(...) will call FigureCanvasBase.init(...) which resets the manager field needed in sca(). Below are the listing showing the problem:

class FigureCanvasBase:
    def __init__(self, figure):
        ...
        figure.set_canvas(self)
        self.figure = figure
        **# Here it is setting canvas.manager to None!!**
        self.manager = None
        ...

def sca(ax):
    """
    Set the current Axes to *ax* and the current Figure to the parent of *ax*.
    """
    if not hasattr(ax.figure.canvas, "manager"):
        raise ValueError("Axes parent figure is not managed by pyplot")
      **# !!! ax.figure.canvas.manager is None!**
    _pylab_helpers.Gcf.set_active(ax.figure.canvas.manager)
    ax.figure.sca(ax)

For workaround, I checked out old version of sca(ax) from 3.2.2 and it seems to be working. On the other hand, I am not sure whether the old version is robust in v3.3.0 despite of this working situation.

def sca(ax):
    """
    Set the current Axes instance to *ax*.

    The current Figure is updated to the parent of *ax*.
    """
    managers = _pylab_helpers.Gcf.get_all_fig_managers()
    for m in managers:
        if ax in m.canvas.figure.axes:
            _pylab_helpers.Gcf.set_active(m)
            m.canvas.figure.sca(ax)
            return
    raise ValueError("Axes instance argument was not found in a figure")

Please check and see whether bring back of old version of sca(ax) is a correct fix of this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions