Thanks to visit codestin.com
Credit goes to github.com

Skip to content

pgf backend no longer supports fig.draw #18407

Closed
@pharshalp

Description

@pharshalp

Bug report

Because of the cleanups in #15977, the pgf backend no longer supports fig.draw(fig.canvas.get_renderer()) to trigger a draw.

If this is not supposed to be a bug and is the correct expected behavior, is there a cleaner way to trigger a draw without using fig.savefig?

Bug summary

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

plt.switch_backend('pgf')  # Switching to the PGF backend
fig, ax = plt.subplots(figsize=(6, 3.0), ncols=2, nrows=1,
                       constrained_layout=True, sharey=True)
fig.set_constrained_layout_pads(wspace=0.03)

ax[0].plot(np.arange(4), 'r', label=r'Really longgggg label 1')
ax[1].plot(np.arange(5), 'b', label=r'Really longgggg label 2')
(leg_h0, leg_lab0) = ax[0].get_legend_handles_labels()
(leg_h1, leg_lab1) = ax[1].get_legend_handles_labels()

# add a legend to one of the axes to make space at the top of the figure
leg0 = ax[0].legend(leg_h0, leg_lab0, loc='lower center',
                    bbox_to_anchor=(0.5, 1.07))

leg0.set_in_layout(True)  # force to steal space from top of the figure
# ------------------------------------------------------
# Trigger a draw (required for constrained_layout to work)
# fig.canvas.draw()   # Doesn't work since pgf is a non-interactive backend
fig.draw(fig.canvas.get_renderer())  # used to work before #15977
# ------------------------------------------------------
# Thanks constrained_layout, your work is done! turning you off now
fig.set_constrained_layout(False)

# Remove legend from axes and add a figure legend
# which includes entries from both axes
leg0.remove() 
fig_leg = fig.legend(*zip(leg_h0, leg_h1), *zip(leg_lab0, leg_lab1),
                     loc='lower center', ncol=2, columnspacing=5,
                     bbox_to_anchor=(0.5, 0.85))
fig.savefig('test.png', dpi=200)

Actual outcome

Traceback (most recent call last):
  File "test.py", line 22, in <module>
    fig.draw(fig.canvas.get_renderer())  # works!
  File "---\Miniconda3\lib\site-packages\matplotlib\artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "---\Miniconda3\lib\site-packages\matplotlib\figure.py", line 1862, in draw
    self.patch.draw(renderer)
  File "---\Miniconda3\lib\site-packages\matplotlib\artist.py", line 41, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "---\Miniconda3\lib\site-packages\matplotlib\patches.py", line 587, in draw
    draw_path(tpath, affine,
  File "---\Miniconda3\lib\site-packages\matplotlib\backends\backend_pgf.py", line 475, in draw_path
    writeln(self.fh, r"\begin{pgfscope}")
  File "---\Miniconda3\lib\site-packages\matplotlib\backends\backend_pgf.py", line 129, in writeln
    fh.write(line)
AttributeError: 'NoneType' object has no attribute 'write'

Expected outcome
test

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.3.1
  • Matplotlib backend (print(matplotlib.get_backend())): pgf
  • Python version: 3.8.5
  • Jupyter version (if applicable):
  • Other libraries:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions