Skip to content

Pixelated fonts when plot saved as jpeg #5302

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

Closed
cbreeden opened this issue Oct 22, 2015 · 2 comments
Closed

Pixelated fonts when plot saved as jpeg #5302

cbreeden opened this issue Oct 22, 2015 · 2 comments
Assignees

Comments

@cbreeden
Copy link

I have noticed that when you save figures as jpeg using the nbagg backend you will get pixelated fonts for labels and tickmarks. It appears this has to do with giving PIL raw data with transparent regions when trying to save jpeg images. This can be avoided with setting: rcParams['nbagg.transparent'] = False.

Is this behavior desired? Perhaps there is a way we can assign default values that will be called for when you have a transparency in a figure and you are trying to save it to a file format that doesn't support transparency?

Please see http://stackoverflow.com/questions/33283274/pixelated-fonts-when-plot-is-saved-as-jpeg for more information.

@tacaswell
Copy link
Member

Odd that it only hits the tick labels, but not the other text.

Can you please move the images and example code over here? I do not trust SO long-term.

@cbreeden
Copy link
Author

Here is code to reproduce the pixelated fonts when saving a figure as a jpeg (when using jupyter):

%matplotlib nbagg

import matplotlib.pyplot as plt
import numpy as np

x    = np.linspace(-1.2,1.2,1000,endpoint=True)
y    = np.copy(x)
x,y  = np.meshgrid(x,y)
z    = -x**2 + y**2 - y**3

fig = plt.figure()
ax  = fig.add_subplot(111)

CS = plt.contour(x,y,z, [0,-0.1,0.1], colors=['black','blue', 'gray'])
plt.clabel(CS, fontsize=14, inline=1, fmt='%1.1f', manual=[(-0.15,0), (-0.4,0), (0.25,0.5)])

plt.title('Contour Sets')

plt.savefig('plot.png', format='png')
plt.savefig('plot.jpg', format='jpg')

This produces a plot.png shown here:

plot

and plot.jpg shown here:

plot

Adding the following code will "fix" this issue and produce jpegs that look nearly identical to the png files (with the undesirable side-effect of have non-transparent figures rendered in your jupyter notebooks):

import matplotlib
matplotlib.rcParams['nbagg.transparent'] = False

Another example for how PIL handles these transparent regions while trying to encode the raw data into jpeg is given by taking the first example and adding the code:

import matplotlib
matplotlib.rcParams['savefig.transparent'] = True

plot.jpg in this case looks like this:

plot_transparent

I'm not exactly sure if this problem should be handled by the PIL team of the matplotlib team, but it might be worth looking into adding code to remove transparent regions when trying to save figures to file formats that can't handle transparency. I don't think it would be a difficult issue to fix, and might be able to write up a pull request for a suggested solution at some point in time.

This issue also seems to surface when trying to create animations.

@mdboom mdboom self-assigned this Oct 26, 2015
@mdboom mdboom closed this as completed in bdea63d Oct 28, 2015
tacaswell added a commit that referenced this issue Oct 28, 2015
Fix #5302: Proper alpha-blending for jpeg
QuLogic pushed a commit to QuLogic/matplotlib that referenced this issue Oct 16, 2016
Fix matplotlib#5302: Proper alpha-blending for jpeg

Conflicts:
	lib/matplotlib/tests/test_image.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants