Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a scatter plot with different edgecolor in matplotlib?

I wish to plot a scatter plot with no facecolors but with different edgecolors. In other words, colorful circles.

I thought I could easily found solution in matplotlib gallery but surprisingly I couldn't. Parameter "edgecolors" does not accept array as values. And manipulate parameter 'c' won't help with edgecolors but facecolors.

Here is a link that I thought would be the solution: https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.scatter.html enter image description here

The demo picture at the end of the page seems to have colorful edgecolors! Then I download the script and run it: enter image description here

The edge is still black.

I am now doing my plot other ways. (since this is not a big matter. This is only a question of interest.) I just wonder how did the demo get the colorful edges.

like image 759
Shuo Yang Avatar asked Jul 21 '17 22:07

Shuo Yang


People also ask

How do I plot multiple Scatterplots in Matplotlib?

Multiple scatter plots can be graphed on the same plot using different x and y-axis data calling the function Matplotlib. pyplot. scatter() multiple times.

How do I plot multiple Y-axis in Matplotlib?

Using subplots() method, create a figure and a set of subplots. Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales. Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2. Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.

Is it possible to create a Coloured scatterplot using Matplotlib?

Scatter Plot Color by Category using Matplotlib Matplotlib scatter has a parameter c which allows an array-like or a list of colors. The code below defines a colors dictionary to map your Continent colors to the plotting colors.


1 Answers

As others have said the way that the demo script got the colourful edges is that matplotlib 2.0 automatically sets the edge colour of the points in the scatter plot to the provided colour argument.

If, however, you want to recreate the results of the demo script without updating to mpl 2.0 (though I recommend it) you only need to add an additional keyword parameter to the scatter line as follows

plt.scatter(x, y, s=area, c=colors, alpha=0.5, edgecolors=colors)

And for the sake of completeness you can implement the colourful circle idea that you originally had by faking it. You only need to change the internal colour to white, i.e.

plt.scatter(x, y, s=area, c='w', alpha=0.5, edgecolors=colors)

Additionally, you can't use a single float to define the colour for edgecolors as they are doing for the 'c' argument in the demo script so you either need to supply an array of length N that are all specified colours, e.g. ['r', 'g', 'b'], or replace the definition of the colours with

colors = np.random.random((N, p))

where p is either 3 or 4 depending if you want RGB or RGBA colours in the plot. This should produce something like the following plot

Output from scatter_demo.py with no face colour and colourful edges

like image 135
JBuete Avatar answered Nov 15 '22 00:11

JBuete