Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: scatter plot with colormaps for edgecolor but no facecolor

I want to have a scatter plot with colormap for edgecolors but no facecolors. When I use facecolor='None', it does not work.

import numpy as np
import matplotlib.pyplot as plt


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radii

plt.scatter(x, y, s=area,c=colors,facecolors='None',cmap="gist_rainbow", alpha=0.5)
plt.show()

Any solution?

like image 264
Mohamad Moosavi Avatar asked Apr 20 '17 12:04

Mohamad Moosavi


People also ask

Can you use matplotlib to create a colored scatterplot?

Matplotlib scatter plot color label To create a scatter plot, we use the scatter() method. To add a legend to a plot, we use the legend() method. To set the color of the legend, we pass facecolor parameter to the method.

How do you change the shape of dots on a scatter plot in Python?

Just use the marker argument of the plot() function to custom the shape of the data points.

What is the difference between PLT plot and PLT scatter?

The primary difference of plt. scatter from plt. plot is that it can be used to create scatter plots where the properties of each individual point (size, face color, edge color, etc.) can be individually controlled or mapped to data.

How to set an edge color of the scatter markers in Matplotlib?

To set an edge color of the scatter markers, use the edgecolor parameter with the scatter () method. Here we pass the parameter edgecolor, color to the scatter () function and set its value to black and lime respectively. Here we’ll see an example, where we set the different edgecolors for each scatter marker. Import library matplotlib.pyplot.

What is a colormap in Matplotlib?

The Matplotlib module has a number of available colormaps. A colormap is like a list of colors, where each color has a value that ranges from 0 to 100. This colormap is called 'viridis' and as you can see it ranges from 0, which is a purple color, and up to 100, which is a yellow color.

How do I change the color of a scatter plot?

You can set your own color for each scatter plot with the color or the c argument: You can even set a specific color for each dot by using an array of colors as value for the c argument: Note: You cannot use the color argument for this, only the c argument. The Matplotlib module has a number of available colormaps.

How to plot a scatter plot using matplotlib in Python?

Import library matplotlib.pyplot. Next, define x and y axes data points. Then create list of colors. To plot a scatter graph, use scatter () function. To set the different edgecolor for each scatter pass edgecolor parameter and set its value to given list of colors. We’ll see an example, where we set a different color for each scatter point.


1 Answers

The c argument will affect facecolor and edgecolor simultaneouly, the arguments facecolor and edgecolor are hence ignored.

A solution would be not to use the c argument together with a colormap, but instead use facecolors and edgecolors alone. In this case facecolors can be set to "None" and edgecolors can be given a list of colors to use.

To create this list, the same colormap can be applied.

c = plt.cm.gist_rainbow(colors)
plt.scatter(x, y, s=area,facecolors="None", edgecolors=c, lw=1,alpha=0.5)

A complete example:

import numpy as np
import matplotlib.pyplot as plt

N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radii

c = plt.cm.gist_rainbow(colors)
plt.scatter(x, y, s=area,facecolors="None", edgecolors=c, lw=2,alpha=0.5)
plt.show()

enter image description here

like image 102
ImportanceOfBeingErnest Avatar answered Sep 29 '22 22:09

ImportanceOfBeingErnest