Skip to content
Advertisement

How to remove edge lines from tripcolor plot with alpha != 0?

The matplotlib.pyplot.tripcolor example produces this image:

If I change the plotting line from

tpc = ax1.tripcolor(triang, z, shading='flat')

to

tpc = ax1.tripcolor(triang, z, shading='flat', alpha=0.5)

then coloured edges appear:

Adding antialiased=True makes things a bit better, but edges are still visible:

Nothing else I tried changed the appearance of the edges. They seem unaffected by setting linewidths or edgecolors, nor by the methods set_linewidth or set_edgewidths on the tpc object.

How can I plot a transparent tripcolor without edges?

Advertisement

Answer

I think it’s kind of inevitable that there will be some overlap or space between non-rectangularly positionned patches on a discrete grid (the image).

For the example case however, there seems to be little reason to use any alpha. Instead, using alpha blending and creating a new colormap with those blended colors, gives the same result.

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
from matplotlib.colors import ListedColormap

n_angles = 36
n_radii = 8
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += np.pi / n_angles

x = (radii * np.cos(angles)).flatten()
y = (radii * np.sin(angles)).flatten()
z = (np.cos(radii) * np.cos(3 * angles)).flatten()

triang = tri.Triangulation(x, y)

triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),
                         y[triang.triangles].mean(axis=1))
                < min_radius)

alpha = 0.5

fig1, (ax1, ax2) = plt.subplots(nrows=2, figsize=(4.5,8))
ax1.set_aspect('equal')
tpc = ax1.tripcolor(triang, z, shading='flat', alpha=alpha, antialiased=True)
fig1.colorbar(tpc, ax=ax1)
ax1.set_title('alpha=0.5')

# Alpha blending
cls = plt.get_cmap()(np.linspace(0,1,256))
cls = (1-alpha) + alpha*cls
cmap = ListedColormap(cls)

ax2.set_aspect("equal")
tpc2 = ax2.tripcolor(triang, z, shading='flat', antialiased=True, linewidth=0.72,
                     edgecolors='face', cmap=cmap)
fig1.colorbar(tpc2, ax=ax2)
ax2.set_title('opaque, alphablending')

fig1.tight_layout()
fig1.savefig("tripcolor.png")
plt.show()

enter image description here

Else, you can of course increase the dpi to insane values to get rid of the spacing. E.g. dpi=1000 (instead of the dpi=72 used in the question) gives a picture with no edges to be observable.

enter image description here

Advertisement