Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 211
Raket Makhim Avatar asked Nov 29 '17 23:11

Raket Makhim


People also ask

How do you plot an empty circle in Python?

How do you plot a hollow circle in Python? Use plt. scatter() to draw empty circles Call plt. scatter(x, y, color=None, edgecolor=None) with x as a sequence of x-coordinates, y as a sequence of y-coordinates, color set to "none" , and edgecolor set to some string representing a color to plot empty circles.


1 Answers

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

like image 197
pault Avatar answered Oct 20 '22 20:10

pault