Skip to content

Border issues when exporting images on version >3.0.3 #16981

Open
@pawelad

Description

@pawelad

Bug report

Bug summary

We noticed weird border issues after upgrading matplotlib from 3.0.3 to higher versions. I tried searching through the open issues but I couldn't find a matching one. I also tried looking through the changelog for 3.1.0 but nothing stood out.

Code for reproduction

This is a bit simplified version of what we're doing on production:

import io

from matplotlib.figure import Figure
from matplotlib.patches import Arc, Rectangle
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

graph_path = "/tmp/foobar.png"

PITCH_WIDTH = 65
PITCH_LENGTH = 105
PITCH_SCALE = 8

# Create example pitch graph
fig = Figure(figsize=(PITCH_WIDTH / PITCH_SCALE, PITCH_LENGTH / PITCH_SCALE))
ax = fig.subplots()
ax.set_facecolor("#1e1e1e")

ax.set_xlim(0, PITCH_WIDTH)
ax.set_ylim(0, PITCH_LENGTH)

pitch_borders = Rectangle(
    (0, 0),
    width=PITCH_WIDTH,
    height=PITCH_LENGTH,
    color="#ffffff",
    fill=False,
    lw=2,
    zorder=10,
)
ax.add_patch(pitch_borders)

halfway_line = Rectangle(
    (0, PITCH_LENGTH / 2),
    width=PITCH_WIDTH,
    height=0,
    color="#ffffff",
    fill=False,
    lw=2,
    zorder=0,
)
ax.add_patch(halfway_line)

centre_circle = Arc(
    (PITCH_WIDTH / 2, PITCH_LENGTH / 2),
    width=18,
    height=18,
    color="#ffffff",
    theta1=0,
    theta2=360,
    lw=2,
    zorder=0,
)
ax.add_patch(centre_circle)

ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# Export it to bytes buffer
buffer = io.BytesIO()
canvas = FigureCanvas(fig)
canvas.print_figure(buffer, dpi=150, bbox_inches="tight", pad_inches=0)
buffer.seek(0)

# Save buffer to a file
with open(graph_path, "wb") as f:
    f.write(buffer.getvalue())

Actual outcome

On versions newer then 3.0.3 you can see weird borders on left and bottom side.

On version 3.1.3:
pitch_v313

On version 3.2.1:
pitch_v321

Expected outcome

On version 3.0.3 it works as expected, without the weird borders:
pitch_v303

We're not sure if it's something we're doing wrong (so a fix for matplotlib was a regression for us) or is there a bigger bug there.

Matplotlib version

  • Operating system: OS X 10.14.6
  • Matplotlib version: 3.2.1 (and 3.1.3)
  • Matplotlib backend (print(matplotlib.get_backend())): this returns TkAgg, but we explicitly use Agg via. FigureCanvasAgg
  • Python version: 3.7.7
  • Jupyter version (if applicable): n/a
  • Other libraries: n/a

Python installed via pyenv, matplotlib installed via pip inside a virtualenv. No other extra libraries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions