-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Issue with the way rasterization works #13718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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? |
In my use case I am actually not using |
Do you have a minimal example for how |
Here is an example which closely resembles my use case: import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['figure.dpi'] = 200
data = np.random.rand(6, 832)
y = np.array([100, 200, 300, 400, 500, 600])
fig1, ax1 = plt.subplots()
for ind in range(data.shape[1]):
ax1.scatter(data[:, ind], y, marker='o', rasterized=True)
fig1.savefig('rasterizing_markers.pdf')
fig2, ax2 = plt.subplots()
ax2.set_rasterized(True)
for ind in range(data.shape[1]):
ax2.scatter(data[:, ind], y, marker='o')
fig2.savefig('rasterizing_axes.pdf') 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 |
Did you try: data = np.random.rand(6, 832)
y = np.array([100, 200, 300, 400, 500, 600])
y = np.tile(y, (1, 832))
fig1, ax1 = plt.subplots()
ax1.scatter(data, y, marker='o', rasterized=True) |
thanks! it worked and makes sense. Should I close the issue? |
Well, it depends on whether you consider this issue to be solved or if there is still something that matplotlib can or should do? |
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? fig, ax = plt.subplots()
ax.set_rasterized(True)
ax.plot([1,2,3]) # plot/scatter/whatever plotting method is needed
fig.canvas.draw() # to trigger tick label formatters
for xlab in ax.get_xticklabels():
xlab.set_rasterized(False)
fig.savefig('file.pdf') xtick labels still end up being rasterized! |
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. |
Thanks everyone! |
Bug report
Bug summary
Rasterizing the marker collection results in a large file size as compared to rasterizing the whole axes. See the example below:
Code for reproduction
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)
todata = 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).Matplotlib version
print(matplotlib.get_backend())
):The text was updated successfully, but these errors were encountered: