8000 constrained-layout gives wrong results when explicitly equal width ratios are set · Issue #17304 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
constrained-layout gives wrong results when explicitly equal width ratios are set #17304
Closed
@anntzer

Description

@anntzer

Bug report

Bug summary

When passing width_ratios = [1, 1, 1] instead of not passing it at all (which should be synonymous), constrained_layout sometimes gives incorrect results.

Code for reproduction

import matplotlib as mpl
from matplotlib import pyplot as plt, gridspec, testing

mpl.rcdefaults()
mpl.style.use(["classic", "_classic_test_patch"])
mpl.testing.set_font_settings_for_testing()

def example_plot(ax, fontsize=12):
    ax.plot([1, 2])
    ax.locator_params(nbins=3)
    ax.set_xlabel('x-label', fontsize=fontsize)
    ax.set_ylabel('y-label', fontsize=fontsize)
    ax.set_title('Title', fontsize=fontsize)

fig = plt.figure(constrained_layout=True)
gs = gridspec.GridSpec(3, 3, figure=fig)

ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1:])
ax3 = fig.add_subplot(gs[1:, 0:2
683A
])
ax4 = fig.add_subplot(gs[1:, -1])

for ax in fig.axes:
    example_plot(ax)

fig.savefig("/tmp/cl1.png")

fig = plt.figure(constrained_layout=True)
gs = gridspec.GridSpec(3, 3, figure=fig,
                       width_ratios=[1, 1, 1], height_ratios=[1, 1, 1])

ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1:])
ax3 = fig.add_subplot(gs[1:, 0:2])
ax4 = fig.add_subplot(gs[1:, -1])

for ax in fig.axes:
    example_plot(ax)

fig.savefig("/tmp/cl2.png")

plt.show()

Actual outcome

cl1.png
cl1

cl2.png
cl2

Clearly in cl2.png the widths are not in the ratios 1:1:1 but (approximately) 1:2:1.

This is likely a floating point error; it is extremely sensitive to conditions, in particular the call to set_font_settings_for_reproducibility (which just sets some font-related rcs) is necessary to observe this.

Expected outcome

twice the same image

Matplotlib version

  • Operating system: archlinux
  • Matplotlib version: master
  • Matplotlib backend (print(matplotlib.get_backend())): agg
  • Python version: 38
  • Jupyter version (if applicable): no
  • Other libraries:

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: geometry managerLayoutEngine, Constrained layout, Tight layout

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0