How to make transparent Matplotlib diagrams transparent as a group?

I am making some scatterplots using Matplotlib (python 3.4.0, matplotlib 1.4.3, running on Linux Mint 17). It is easy enough to set alpha transparency for each point separately; is there a way to set them as a group so that two overlapping points from the same group do not change color?

Code example:

import matplotlib.pyplot as plt import numpy as np def points(n=100): x = np.random.uniform(size=n) y = np.random.uniform(size=n) return x, y x1, y1 = points() x2, y2 = points() fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, title="Test scatter") ax.scatter(x1, y1, s=100, color="blue", alpha=0.5) ax.scatter(x2, y2, s=100, color="red", alpha=0.5) fig.savefig("test_scatter.png") 

The results of this conclusion:

enter image description here

but I want something more like this:

enter image description here

I can get around by saving as SVG and manually grouping then in Inkscape and then setting transparency, but I would prefer something that I can encode. Any suggestions?

+7
python matplotlib transparency scatter-plot
source share
2 answers

Yes, an interesting question. You can get this scatter plot using Shapely . Here is the code:

 import matplotlib.pyplot as plt import matplotlib.patches as ptc import numpy as np from shapely.geometry import Point from shapely.ops import cascaded_union n = 100 size = 0.02 alpha = 0.5 def points(): x = np.random.uniform(size=n) y = np.random.uniform(size=n) return x, y x1, y1 = points() x2, y2 = points() polygons1 = [Point(x1[i], y1[i]).buffer(size) for i in range(n)] polygons2 = [Point(x2[i], y2[i]).buffer(size) for i in range(n)] polygons1 = cascaded_union(polygons1) polygons2 = cascaded_union(polygons2) fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, title="Test scatter") for polygon1 in polygons1: polygon1 = ptc.Polygon(np.array(polygon1.exterior), facecolor="red", lw=0, alpha=alpha) ax.add_patch(polygon1) for polygon2 in polygons2: polygon2 = ptc.Polygon(np.array(polygon2.exterior), facecolor="blue", lw=0, alpha=alpha) ax.add_patch(polygon2) ax.axis([-0.2, 1.2, -0.2, 1.2]) fig.savefig("test_scatter.png") 

and the result:

Test scatter

+5
source share

An interesting question, I think that any use of transparency will lead to a styling effect that you want to avoid. You can manually set the transparency color to get closer to the desired results,

 import matplotlib.pyplot as plt import numpy as np def points(n=100): x = np.random.uniform(size=n) y = np.random.uniform(size=n) return x, y x1, y1 = points() x2, y2 = points() fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, title="Test scatter") alpha = 0.5 ax.scatter(x1, y1, s=100, lw = 0, color=[1., alpha, alpha]) ax.scatter(x2, y2, s=100, lw = 0, color=[alpha, alpha, 1.]) plt.show() 

The overlay between different colors does not turn on this way, but you get

enter image description here

+3
source share

All Articles