You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importnumpyasnpimportpyplotasplt.
X, Y=np.meshgrid([1, 2], [3, 4])
pc=plt.pcolormesh(X, Y, [[1]])
pc.set_rasterized(True) # colorbar does thisplt.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.
@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?
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.
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.
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.
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.
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.
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.
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,
but if you highlight the boundary of the actual image in Inkscape, you can see the error is just as bad:
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....
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.
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!
Activity
efiring commentedon Jul 25, 2016
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:
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.
astrofrog commentedon Jul 25, 2016
@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?
astrofrog commentedon Jul 25, 2016
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.
efiring commentedon Jul 25, 2016
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.
QuLogic commentedon Jul 27, 2016
Could you use
matplotlib.image.NonUniformImage?efiring commentedon Jul 27, 2016
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.
tacaswell commentedon Jul 28, 2016
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.
jkseppan commentedon Dec 26, 2016
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.
tacaswell commentedon Dec 26, 2016
Could this be yet another integer snapping issue?
16 remaining items
jklymak commentedon Apr 17, 2020
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.
brunobeltran commentedon Apr 17, 2020
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 thedraw_imagepipeline makes sure to clip the image to the axes in that case.produces a figure that only looks a "little" off,
but if you highlight the boundary of the actual image in Inkscape, you can see the error is just as bad:
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....jklymak commentedon Apr 17, 2020
I don't think its by chance that the bottom left corner aligns? The image origin is placed exactly there, isn't it?
brunobeltran commentedon Apr 17, 2020
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).
jklymak commentedon Apr 17, 2020
I'd have to look at the code, but there is definitely a floating point x and y and a translate command in there...
brunobeltran commentedon Apr 17, 2020
I see, it's a different bug. Unlike the colorbar, the
AxesImageis not processed byMixedModeRenderer.stop_rasterizing(becauseAxesImage.get_rasterized()is False), so its positioncould 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.github-actions commentedon Mar 27, 2023
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!
tacaswell commentedon Apr 27, 2023
attn @QuLogic Please re-open if you think this is related to the DPI work you are doing.
QuLogic commentedon May 5, 2023
I'm not sure it's related to my work, but probably to @jklymak's since he also added the
keeplabel.