Skip to content

AttributeError: 'Figure' object has no attribute '_cachedRenderer' #17627

@mapfiable

Description

@mapfiable

Bug report

Bug summary

I was trying to follow the first suggestion of this post, and copy a figure using pickle. In my example however, this results in an AttributeError: 'Figure' object has no attribute '_cachedRenderer'.

Code for reproduction

import io
import pickle
import numpy as np
from copy import copy, deepcopy
from contextlib import contextmanager
import matplotlib.pyplot as plt


@contextmanager
def copy_figure(fig: plt.Figure):
    buf = io.BytesIO()
    pickle.dump(fig, buf)
    buf.seek(0)
    fig_copy = pickle.load(buf)
    # fig_copy = copy(fig)  # you get the same error when using copy instead
    # fig_copy = deepcopy(fig)  # this results in an "NotImplementedError: 
    #    TransformNode instances can not be copied. Consider using frozen() 
    #    instead." which I have no idea what that means either.
    yield fig_copy
    plt.close(fig_copy)


def example():
    image = np.random.uniform(0, 1, (100, 100))
    fig, ax = plt.subplots()
    image_artist = ax.imshow(image)
    fig.colorbar(image_artist)
    with copy_figure(fig) as copy_fig:
        copy_fig.show()

    fig.show()


example()

I am not sure if this is wanted behaviour, and why this even happens, but if it is not supposed to happen, I guess it would be a simple fix to change this line in tight_layout.get_renderer

    if fig._cachedRenderer:

to this line:

    if hasattr(fig, '_cachedRenderer') and fig._cachedRenderer:

Matplotlib version

  • Operating system: Windows 10 Home 64bit
  • Matplotlib version: 3.1.3 (via Anaconda)
  • Matplotlib backend: backend_interagg
  • Python version: 3.7.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy