Skip to content

Pixelated fonts when plot saved as jpeg #5302

Closed

Description

@cbreeden

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.

Activity

  1. tacaswell commented on Oct 24, 2015

    @tacaswell
    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.

  2. cbreeden commented on Oct 25, 2015

    @cbreeden
    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.

  3. self-assigned this
    on Oct 26, 2015
  4. added a commit that references this issue on Oct 26, 2015
    bdea63d
  5. added a commit that references this issue on Oct 28, 2015
    b9957b1
  6. added a commit that references this issue on Oct 16, 2016
    1e437d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions