Skip to content

Bug with colorbar and low DPI output in PDF #6827

Open

Description

@astrofrog

The following example (with Matplotlib 2.0.0b3) causes the colorbar inside the colorbar axes to be offset when outputting to PDF with a low DPI:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
im = ax.imshow([[1]])
fig.colorbar(im)
fig.savefig('colorbar_test.pdf', dpi=7)

colorbar_test

This wasn't an issue with Matplotlib 1.4:

colorbar_test

and the bug was apparently introduces in Matplotlib 1.5:

colorbar_test

Activity

  1. efiring commented on Jul 25, 2016

    @efiring
    Member

    The problem is that in the usual case with more than 50 colors in the colormap, the colorbar pcolormesh output is rasterized to eliminate the artifacts that would otherwise appear when the pdf is rendered. Rasterizing at 7 dpi gives terrible resolution, so the colorbar interior doesn't coincide with the vector graphics of its outline.
    You can see this with plain pcolormesh:

    import numpy as np
    import pyplot as plt.
    X, Y = np.meshgrid([1, 2], [3, 4])
    pc = plt.pcolormesh(X, Y, [[1]])
    pc.set_rasterized(True)   # colorbar does this
    plt.savefig('test7r.pdf', dpi=7)

    test7r.pdf
    Some degree of mis-registration is inevitable with this technique of rasterization to avoid the artifacts, but it only becomes a problem with low dpi--which I imagine to be an unusual situation.

  2. astrofrog commented on Jul 25, 2016

    @astrofrog
    ContributorAuthor

    @efiring - interestingly, this doesn't affect the main image though. Maybe the colorbar could be rasterized in the same way as imshow rather than using pcolormesh?

  3. astrofrog commented on Jul 25, 2016

    @astrofrog
    ContributorAuthor

    The reason I ran into this problem is that the APLpy package (https://github.com/aplpy/aplpy) basically picks a DPI that matches the imshow resolution to minimize EPS/PDF file size. If this can't be fixed in Matplotlib then the plan B will be to make sure there is a minimum DPI for APLpy figures.

  4. efiring commented on Jul 25, 2016

    @efiring
    Member

    We can't use imshow because the spacing of the color intervals can be nonuniform. Probably we should use image.PcolorImage (directly, or via Axes.pcolorfast) instead of pcolormesh; but a quick check indicates that this doesn't solve the problem. This looks like a difficult problem to solve; one way or another, rasterization of an artist in the vector backends must be restricted to cases where the dpi is reasonably large. Maybe that logic can be put in the backends.

  5. QuLogic commented on Jul 27, 2016

    @QuLogic
    Member

    Could you use matplotlib.image.NonUniformImage?

  6. efiring commented on Jul 27, 2016

    @efiring
    Member

    No, PcolorImage is a modification of NonUniformImage that has the right property for a colorbar: it colors blocks based on specified boundaries rather than centers. The rendering goes through the same code path.

  7. tacaswell commented on Jul 28, 2016

    @tacaswell
    Member

    attn @jkseppan @mdboom

    I do not understand what is going on here. The artists are rasterized at a DPI which is independent of the vector dpi. This looks like there is a quantization issue going on with placing the raster image back into the vector backend rather than an inherent bug.

  8. added this to the milestone on Jul 28, 2016
  9. modified the milestones: , on Nov 10, 2016
  10. jkseppan commented on Dec 26, 2016

    @jkseppan
    Member

    I agree that this looks like a problem in placing the rasterized image. The code in backend_mixed.py switches around the figure dpi in a way that I find suspicious, but I don't see an obvious bug there either.

  11. tacaswell commented on Dec 26, 2016

    @tacaswell
    Member

    Could this be yet another integer snapping issue?

  12. modified the milestones: , on May 3, 2017
  13. 16 remaining items

  14. jklymak commented on Apr 17, 2020

    @jklymak
    Member

    dpi need not be an integer, it just happened to be above. If an object is 1.637 inches wide, and dpi is set at 100, then we would use 164 pixels to represent it across 1.637 inches for a practical dpi of 100.18 dpi.

  15. brunobeltran commented on Apr 17, 2020

    @brunobeltran
    Contributor

    While I'm fixing this, might as well note for posterity that contrary to what was reported originally, this bug is also present for imshow, it's just hidden better because the draw_image pipeline makes sure to clip the image to the axes in that case.

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    
    mpl.rcParams['figure.dpi'] = 20
    fig, ax = plt.subplots(figsize=(2, 1))
    
    checkers = np.array([[0, 1], [0, 1]])
    ax.imshow(checkers)
    
    plt.savefig('/home/bbeltr1/Downloads/test-imshow.svg')
    

    produces a figure that only looks a "little" off,

    test-imshow svg

    but if you highlight the boundary of the actual image in Inkscape, you can see the error is just as bad:

    bitmap

    The rasterized element <svg:image> has a height and width of 57.6pts, whereas the axes containing it are only 54.36-by-54.36. Since the rounding error more or less aligns the bottom left corner in this case (by chance), this is also noticeable by the fact that the centerline is not quite centered....

  16. jklymak commented on Apr 17, 2020

    @jklymak
    Member

    I don't think its by chance that the bottom left corner aligns? The image origin is placed exactly there, isn't it?

  17. brunobeltran commented on Apr 17, 2020

    @brunobeltran
    Contributor

    Oh I guess in this case it is exactly there?

    I just assumed it was subject to the same integer clipping issues as in the colorbar case (where the bottom corner typically does not align).

  18. jklymak commented on Apr 17, 2020

    @jklymak
    Member

    I'd have to look at the code, but there is definitely a floating point x and y and a translate command in there...

  19. brunobeltran commented on Apr 17, 2020

    @brunobeltran
    Contributor

    I see, it's a different bug. Unlike the colorbar, the AxesImage is not processed by MixedModeRenderer.stop_rasterizing (because AxesImage.get_rasterized() is False), so its position could in principle be correct, would have to dig down that code path independently to figure out why its size is wrong when its position isn't.... is correct. PR incoming.

  20. github-actions commented on Mar 27, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  21. tacaswell commented on Apr 27, 2023

    @tacaswell
    Member

    attn @QuLogic Please re-open if you think this is related to the DPI work you are doing.

  22. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Apr 27, 2023
  23. reopened this on May 5, 2023
  24. QuLogic commented on May 5, 2023

    @QuLogic
    Member

    I'm not sure it's related to my work, but probably to @jklymak's since he also added the keep label.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions