Skip to content
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

matplotlib svg save - light borders around contourf levels #4419

Closed
cattt84 opened this issue May 12, 2015 · 2 comments
Closed

matplotlib svg save - light borders around contourf levels #4419

cattt84 opened this issue May 12, 2015 · 2 comments

Comments

@cattt84
Copy link

cattt84 commented May 12, 2015

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')
@tacaswell tacaswell added this to the next point release milestone May 12, 2015
@jenshnielsen
Copy link
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.

@tacaswell tacaswell modified the milestones: next point release, proposed next point release Jul 11, 2015
@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@QuLogic QuLogic self-assigned this Jul 21, 2020
@QuLogic QuLogic mentioned this issue Apr 2, 2021
7 tasks
@QuLogic
Copy link
Member

QuLogic commented Mar 4, 2023

Closing as a duplicate of #1188.

@QuLogic QuLogic closed this as completed Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants