Skip to content

[Bug]: Irregular pixel widths in rasterized pcolor #23391

Closed as not planned
Closed as not planned

Description

@mschroen

Bug summary

Pixel widths appear irregularly in pcolor(x, y, z, rasterized=True), while x and y are regular. Using rasterized=False provides correct (equal size) pixels, but saving the figure to .png (instead of .pdf) will show the same irregular pixel width bug.

It looks like a resampling bug, which messes up precise data visualization completely.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
x, y = np.meshgrid(range(500), range(2))
z = [np.tile([0,1],250), np.tile([0,1],250)]
fig, ax = plt.subplots(1,1, figsize=(10,2))
im = ax.pcolor(x, y, z, rasterized=True)
ax.set_xticks(np.arange(0,40))
fig.savefig('test.pdf')
plt.show()

Actual outcome

In the resulting plot, some pixels are thinner, and some are thicker, than they should be.
rasterize=True (zoom-in to the lower left corner)
grafik

Expected outcome

rasterize=False (zoom-in to the lower left corner)
grafik

Additional information

  • This behaviour depends on the number of pixels plotted, for me it becomes visible above len(x)>100.
  • The spread between thicker and thinner lines increases with increasing size of the input array.
  • If the view is reduced to a lower number of pixels (e.g., ax.set_xlim(0,10)), the effect is no longer visible.

Operating system

Windows

Matplotlib Version

3.5.1

Matplotlib Backend

inline

Python version

3.9.6

Jupyter version

No response

Installation

pip

Activity

  1. jklymak commented on Jul 6, 2022

    @jklymak
    Member

    I think this is basically #17182

  2. jklymak commented on Jul 7, 2022

    @jklymak
    Member

    I'm going to close as an understood problem tracked in #6827 among other places. Yes this is very annoying, and there is a partial solution that we hope to implement soon... Thanks,

  3. mschroen commented on Jul 7, 2022

    @mschroen
    Author

    Thanks for working on a solution. The referenced issues seem to deal with border pixels and overlapping, rather than constant size pixels, but I understand if this is related at the very bottom of the code implementation. Nevertheless, I don't see why this is a reason to close (~mark as solved) this issue?

  4. jklymak commented on Jul 7, 2022

    @jklymak
    Member

    We have over 1.5k issues so we try and cut down on duplicates at the triage stage.

    Also for the behaviour you are describing, you should play with the various places "snap" can be set. https://matplotlib.org/stable/api/_as_gen/matplotlib.artist.Artist.set_snap.html

  5. tacaswell commented on Jul 7, 2022

    @tacaswell
    Member

    Try passing

    fig.savefig('test.pdf', dpi=300)
    

    which should increase the effective dpi that the artists are rasterized at (by default it as at 72 dpi).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions