GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Scale an image to the axes edges regardless of whether it is being plotted with a nonaffine transform or an affine transform by ayshih · Pull Request #30175 · matplotlib/matplotlib

Conversation

@ayshih

@ayshih ayshih commented Jun 14, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

PR summary

This PR fixes a difference between plotting an image with a nonaffine transform versus with an affine transform. When plotting with an affine transform, the transform for rendering gets an additional transform step to scale the image to the axes edges in the situation where the axes bbox is not a whole number of pixels. However, when plotting with a nonaffine transform, this additional scaling step is skipped, and instead the fractional pixel at the end is clipped off. I can't think of any reason why this difference is desirable, so this PR removes the different handling of a nonaffine transform versus an affine transform.

Before this PR

Here's an example image before this PR and generating script. Note that the image plotted using the nonaffine transform has a obvious gap to the axis on the right edge due to the fractional pixel. The discrepancy on the top edge is not quite as obvious.
Figure_1

import numpy as np

import matplotlib.pyplot as plt
from matplotlib.transforms import Transform

# Create a nonaffine identity transform
class NonAffineIdentityTransform(Transform):
    input_dims = 2
    output_dims = 2

    def inverted(self):
       return self
nonaffine_transform = NonAffineIdentityTransform()

data = np.arange(16).reshape((4, 4)) % 3

fig, axs = plt.subplots(2, figsize=(6, 3), dpi=100)

# Choose axes bboxs with with fractional pixels
axs[0].set_position([0.1, 0.15, 200.75 / fig.bbox.width, 200.75 / fig.bbox.height])
axs[1].set_position([0.6, 0.15, 200.75 / fig.bbox.width, 200.75 / fig.bbox.height])

im0 = axs[0].imshow(data)
im1 = axs[1].imshow(data, transform=nonaffine_transform + axs[1].transData)

axs[0].spines[:].set_ls((0, (5, 5)))
axs[1].spines[:].set_ls((0, (5, 5)))

axs[0].set_title("Affine")
axs[1].set_title("Nonaffine")

plt.show()

After this PR

after

PR checklist

  • "closes #0000" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • Plotting related features are demonstrated in an example
  • New Features and API Changes are noted with a directive and release note
  • Documentation complies with general and docstring guidelines

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot added the topic: images label Jun 14, 2025
@ayshih
ayshih force-pushed the nonaffine_scaling branch 4 times, most recently from 59bd52f to 8520a10 Compare June 20, 2025 16:50
@ayshih
ayshih force-pushed the nonaffine_scaling branch from 8520a10 to 0a9c588 Compare October 8, 2025 18:23
@github-actions github-actions Bot added the topic: mplot3d label Oct 8, 2025
@github-actions github-actions Bot added the status: needs rebase label Jan 8, 2026
@ayshih
ayshih force-pushed the nonaffine_scaling branch from 0a9c588 to ea140ec Compare January 15, 2026 20:57
@github-actions github-actions Bot removed topic: mplot3d status: needs rebase topic: images labels Jan 15, 2026
ayshih added 2 commits January 16, 2026 09:29
@ayshih
Add test to check for consistency in scaling images to axes edges
36f27cc
@ayshih
Remove different handling for nonaffine transforms
7abdf72
@ayshih
ayshih force-pushed the nonaffine_scaling branch from 84fc19d to 7abdf72 Compare January 16, 2026 14:30
@github-actions github-actions Bot added the topic: images label Jan 16, 2026
@QuLogic QuLogic mentioned this pull request Jan 24, 2026
Fixed inaccurate image placement and even more resampling bugs #31021
Merged
3 tasks done
@ayshih ayshih closed this Jan 24, 2026
@rcomer rcomer added the status: superseded label Jan 24, 2026
@ayshih
ayshih deleted the nonaffine_scaling branch March 2, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

status: superseded topic: images

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@ayshih @rcomer
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.