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

Skip to content

[Bug]: savefig() breaks FancyArrowPatch position on saved PNG when using custom DPI #31536

@Dephrilibrium

Description

@Dephrilibrium

Bug summary

Heyho,

I encountered an error with patches (FancyArrowPatch).
When i draw FancyArrowPatches into a figure and save the figures with savefig(), the patches shift around to different positions depending on my set dpi=parameter as shown by the following screenshots, produced by code underneath.

Code for reproduction

import os
from os.path import dirname

import copy

import matplotlib.pyplot as plt
import matplotlib.patches as patches



os.chdir(dirname(__file__))
print(f"Changed WD to PyFile-root \"{os.getcwd()}\"")


Color1 = "#1776D4"
Color2 = "#E27A12"

fig, axL = plt.subplots(nrows=1, ncols=1)
axR = axL.twinx()

plt.show(block=False)


arrowUpBG = patches.FancyArrowPatch(
    posA=(0.500, 0.535),        # Startpoint Arrow  (0..1)
    posB=(0.560, 0.560),        # Endpoint Base     (0..1)
    transform=fig.transFigure,  # Coordinate system of figure
    arrowstyle='fancy',
    color=Color1,
    linewidth=10,
    mutation_scale=30
)

# NOTE: Copies have to be made before first draw! Otherwise figure gets cloned too!
arrowUpFG = copy.deepcopy(arrowUpBG)
arrowUpFG.set_color(Color2)
arrowUpFG.set_linewidth(1)

arrowDownBG = copy.deepcopy(arrowUpBG)
arrowDownFG = copy.deepcopy(arrowUpFG)
arrowDownBG.set_positions(posA=(0.590, 0.745),   # Startpoint Arrow  (0..1)
                          posB=(0.535, 0.690),   # Endpoint Base     (0..1)
                          )
arrowDownFG.set_positions(posA=(0.590, 0.745),
                          posB=(0.535, 0.690),
                          )

# NOTE: Draw is here (together, copies have to be made beforehand, as otherwise savefig gets cloned too!)
fig.add_artist(arrowUpBG)
fig.add_artist(arrowUpFG)

fig.add_artist(arrowDownBG)
fig.add_artist(arrowDownFG)



fig.savefig("./FancyIssue - SVG.svg")
fig.savefig("./FancyIssue - Std dpi.png")
fig.savefig("./FancyIssue - 200 dpi.png", dpi=200)
fig.savefig("./FancyIssue - 300 dpi.png", dpi=300)
fig.savefig("./FancyIssue - 600 dpi.png", dpi=600)

print("EOS")

Actual outcome

DPI Standard 200 300 600 SVG (in Firefox)
Imgs Image Image Image Image Image

Expected outcome

Patches stay in relative position to the figure size.
Note: The patches are drawn correctly in the preview (plt.show()).

Additional information

No response

Operating system

Windows 11 (24H2)

Matplotlib Version

3.10.8

Matplotlib Backend

tkagg

Python version

3.14.3

Jupyter version

N/A

Installation

pip

Best regards,
Deph

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