Matplotlib svg save - light borders around outline levels

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

When it comes to saving, I'm not happy with the result. Saving PNG looks perfect, but I don't have any level like png si without vector format. When saving as SVG PDF I have levels BUT there are thin light borders around the levels. At first I thought they were caused because each level is collapsing. Opening the SVG file with inkscape to discard those strokes, I found out that in fact the levels are kept just a little, or too small or too large, respectively ... you hardly notice them when you zoom in, but when you zoom in they become very noticeable. I guess they are due to the fact that the level values ​​are stored with low precision !? Is it possible to get rid of these boundaries by some team?

I know these boundaries won't matter in most application contexts. Unfortunately, where I use them don't just look ugly, but really break the quality of the displayed 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')

      

saved as PNGsaved as SVG

+3


source to share





All Articles