Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Matplotlib scatterplots transparent as a group?

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

Example code:

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") 

Results in this output:

enter image description here

but I want something more like this one:

enter image description here

I can workaround by saving as SVG and manually grouping then in Inkscape, then setting transparency, but I'd really prefer something I can code. Any suggestions?

like image 606
Jason Avatar asked May 07 '15 17:05

Jason


People also ask

How do I change the transparency of a scatter plot in Python?

Adjust the Transparency of Scatter PointsUtilize the alpha argument in our scatter method and pass in a numeric value between 0 and 1. A value of 0 will make the plots fully transparent and unable to view on a white background. A value of 1 is the default with non-transparent points.

How do I change the transparency in Matplotlib?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.

How do I make data points transparent?

A useful tip is to increase the transparency of the colour of the data points so that they can be distinguished more easily. To do this, simply go to the Marks card and click on Colour. Here you can adjust the transparency either by moving the slider or typing in a specific value.

How do I overlay two Scatterplots in Matplotlib?

You simply call the scatter function twice, matplotlib will superimpose the two plots for you. You might want to specify a color, as the default for all scatter plots is blue. This is perhaps why you were only seeing one plot. Save this answer.


1 Answers

Yes, interesting question. You can get this scatterplot with 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 is :

Test scatter

like image 98
Flabetvibes Avatar answered Sep 20 '22 00:09

Flabetvibes