Skip to content

matplotlib svg save - light borders around contourf levels #4419

Closed

Description

@cattt84

The following code is taken in part from the contour demo of the matplotlib documentation. I am using contourf instead of simple contour. The contour plot is shown just as I want it to be within the matplotlib figure window.

As soon as it comes to saving I am not content with the result. A PNG save looks perfect, but I do not have any levels, as png si no vector format. When saving to PDF of SVG format I have levels, BUT there are thin light borders around the levels. At first I thought they are caused, because every level is getting a stroke around. When opening the SVG file with inkscape to drop those strokes, I found out, that actually the levels are saved just a bit to small or a bit too large respectively... you hardly note them, when you zoom in, but by zooming out they get quite prominent. I suppose they are due to the fact, that the values of the levels are saved with low precision!? Is it possible to get rid of theese borders by some command?

I am aware that these borders will not make a difference in most applicative contexts. Unfortunately where I am using them the do not simply look ugly, but really disturb the quality of the depicted results...

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)

plt.ion()
plt.figure()
CS = plt.contourf(X, Y, Z, colors=[[0,0,0.5],[0,0,0.2]])
plt.title('Saved as PNG')
plt.savefig('image1.png')

plt.title('Saved as SVG')
plt.savefig('image1.svg')

Activity

  1. added this to the milestone on May 12, 2015
  2. jenshnielsen commented on May 12, 2015

    @jenshnielsen
    Member

    This is a limitation of the vector backends. When rendering 2 surfaces next to each other without overlap round off errors will produce gaps between them. See for instance #1188

    As a workaround you can add an edge with the same colour around the patches
    The following works for me:

    plt.figure()
    CS = plt.contourf(X, Y, Z, colors=[[0,0,0.5],[0,0,0.2]])
    for a in CS.collections:
        a.set_edgecolor('face')
    plt.title('Saved as PNG')
    plt.savefig('image1.png')
    
    plt.title('Saved as SVG')
    plt.savefig('image1.svg')
    

    However note that this would produce similar ugly overlaps if you use it on a semi transparent surface (alpha < 1) due to the overlaps. This is why it is not on by default.

  3. modified the milestones: , on Jul 11, 2015
  4. modified the milestones: , on Oct 3, 2017
  5. self-assigned this
    on Jul 21, 2020
  6. QuLogic commented on Mar 4, 2023

    @QuLogic
    Member

    Closing as a duplicate of #1188.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions