Plotting circles with no fill, colour & size depending on variables using scatter

Raket Makhim picture Raket Makhim · Nov 30, 2017 · Viewed 18.2k times · Source

The information I have to show on a plot are 2 coordinates: size & colour (no fill). The colour is important as I need a colormap type of graph to display the information depending on a colour value.

I went about trying two different ways of doing this:

  1. Create specific circles and add the individual circles.

    circle1 = plt.Circle(x, y, size, color='black', fill=False)
            ax.add_artist(circle1)
    

The problem with this method was that I could not find a way to set the colour depending on a colour value. i.e. for a value range of 0-1, I want 0 to be fully blue while 1 to be fully red hence in between are different shades of purple whose redness/blueness depend on how high/low the colour value is.

  1. After that I tried using the scatter function:

    size.append(float(Info[i][8]))
    plt.scatter(x, y, c=color, cmap='jet', s=size, facecolors='none')
    

The problem with this method was that the size did not seem to vary, it could possibly be cause of the way I've created the array size. Hence if I replace the size with a big number the plot shows coloured in circles. The facecolours = 'none' was meant to plot the circumference only.

Answer

pault picture pault · Nov 30, 2017

I believe doing both approaches may achieve what you are trying to do. First draw the unfilled circles, then do a scatter plot with the same points. For the scatter plots, make the size 0 but use it to set the colorbar.

Consider the following example:

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm

%matplotlib inline

# generate some random data
npoints = 5
x = np.random.randn(npoints)
y = np.random.randn(npoints)

# make the size proportional to the distance from the origin
s = [0.1*np.linalg.norm([a, b]) for a, b in zip(x, y)]
s = [a / max(s) for a in s]  # scale

# set color based on size
c = s
colors = [cm.jet(color) for color in c]  # gets the RGBA values from a float

# create a new figure
plt.figure()
ax = plt.gca()
for a, b, color, size in zip(x, y, colors, s):
    # plot circles using the RGBA colors
    circle = plt.Circle((a, b), size, color=color, fill=False)
    ax.add_artist(circle)

# you may need to adjust the lims based on your data
minxy = 1.5*min(min(x), min(y))
maxxy = 1.5*max(max(x), max(y))
plt.xlim([minxy, maxxy])
plt.ylim([minxy, maxxy])
ax.set_aspect(1.0)  # make aspect ratio square

# plot the scatter plot
plt.scatter(x,y,s=0, c=c, cmap='jet', facecolors='none')
plt.grid()
plt.colorbar()  # this works because of the scatter
plt.show()

Example plot from one of my runs:

Example plot output