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
Actual outcome
rasterizing_axes.pdf is around 123 KB and rasterizing_markers.pdf is around 447 KB! (the only difference in those two files is that rasterizing_markers.pdf has spines, ticks and tick labels in vector format)
Expected outcome
Expecting that both files will have similar size. I am sure that the difference is not because of the embedded fonts (If I change data = np.random.rand(100, 100) to data = np.random.rand(10, 10), the file size for rasterizing_markers.pdf is 23 KB so that rules out the role of embedded fonts in explaining the ~300 KB difference).
ax1.plot(np.random.rand(100, 100), 'o', rasterized=True) creates 100 "lines", and hence 100 individual rasterized images. If instead you flatten the array, to create only one single "line", ax1.plot(np.random.rand(100, 100).flatten(), 'o', rasterized=True) the filesize reduces tremendously (in my case from 800kb to 20kb).
I'd say, usually one would plot such bunchs of plots via collections. Is there any special use case that would require you to plot that many lines?
Do you have a minimal example for how scatter is used in that case? ax1.scatter(*np.random.rand(2, 5000), marker='o', rasterized=True) would create a 37kb file (compared to a 64kb file in case the axes is rasterized) for me, so it doesn't look like a problem at all.
I think this faces the same issue as you described earlier... this is creating 832 rasterized images resulting in large file size.
I tried coming up with a workaround i.e. starting with ax2.set_rasterized(True) followed by ax2.xaxis.set_rasterized(False) etc.(before savefig but after the loop containing ax2.scatter) to disable rasterization on x/yaxis and spines. But this approach did not work.
In the last case I suspect (with out digging into the code to check) it is due to the way the draw tree works. At the top level we draw the figure by recursively walking the artist tree depth-first. We have code paths in the vector backends to switch temporarily rasterizing an Artist (and implicitly all of it's children) but not a way to switch back to using a vector backend on those children. The tick labels are children of the axis objects which are children of the Axes object.
Activity
ImportanceOfBeingErnest commentedon Mar 20, 2019
ax1.plot(np.random.rand(100, 100), 'o', rasterized=True)creates 100 "lines", and hence 100 individual rasterized images. If instead you flatten the array, to create only one single "line",ax1.plot(np.random.rand(100, 100).flatten(), 'o', rasterized=True)the filesize reduces tremendously (in my case from 800kb to 20kb).I'd say, usually one would plot such bunchs of plots via collections. Is there any special use case that would require you to plot that many lines?
pharshalp commentedon Mar 20, 2019
In my use case I am actually not using
ax.plot. I am using a scatter plot (withax.scatter) to show 5000 points leading to a similar file size issue.ImportanceOfBeingErnest commentedon Mar 20, 2019
Do you have a minimal example for how
scatteris used in that case?ax1.scatter(*np.random.rand(2, 5000), marker='o', rasterized=True)would create a 37kb file (compared to a 64kb file in case the axes is rasterized) for me, so it doesn't look like a problem at all.pharshalp commentedon Mar 20, 2019
Here is an example which closely resembles my use case:
I think this faces the same issue as you described earlier... this is creating 832 rasterized images resulting in large file size.
I tried coming up with a workaround i.e. starting with
ax2.set_rasterized(True)followed byax2.xaxis.set_rasterized(False)etc.(before savefig but after the loop containing ax2.scatter) to disable rasterization on x/yaxis and spines. But this approach did not work.jklymak commentedon Mar 20, 2019
Did you try:
pharshalp commentedon Mar 20, 2019
thanks! it worked and makes sense. Should I close the issue?
ImportanceOfBeingErnest commentedon Mar 20, 2019
Well, it depends on whether you consider this issue to be solved or if there is still something that matplotlib can or should do?
pharshalp commentedon Mar 20, 2019
the only issue that remains (which came up when I was trying out approaches to solve the current issue) is, why doesn't the following work?
xtick labels still end up being rasterized!
tacaswell commentedon Mar 20, 2019
In the last case I suspect (with out digging into the code to check) it is due to the way the draw tree works. At the top level we draw the figure by recursively walking the artist tree depth-first. We have code paths in the vector backends to switch temporarily rasterizing an Artist (and implicitly all of it's children) but not a way to switch back to using a vector backend on those children. The tick labels are children of the axis objects which are children of the Axes object.
pharshalp commentedon Mar 20, 2019
Thanks everyone!