1

For a set of points generated,

  1. How do I plot a circle with different colour at each point?
  2. How do I show the center point of the circle even when circle is filled?

Purpose: I have a set of GPS points and I want to plot the line through these points and for each point, draw a circle of some radius with that point as the center. The colour differentiation is to distinguish the various points and circles that are plotted.

At the moment, I'm only able to plot the points and plot circles at each point with 1 particular colour. (I read other posts about using colour maps and RGB's but unfortunately, I was not able to implement it)

Image below shows the expected plot of circles with different colours (Any colour can be used) for each plot point with the point on the circles representing the center of each circle (At each plot point basically).

Circles different colours

import matplotlib.pyplot as plt
import numpy as np
xx = np.linspace(0,10,5)
yy = np.linspace(30,60,5)
zz = np.vstack((xx,yy)).T
fig = plt.figure(1, figsize=(12, 5))
ax = fig.add_subplot(111, aspect='equal')
plt.plot(xx,yy,zorder=2)
plt.scatter(xx,yy,zorder=1)
for i in range(0,len(xx)):
    circle1 = plt.Circle(zz[i], 4, color='g', fill=True)
    plt.gca().add_patch(circle1)
plt.autoscale()
plt.show()

UPDATE

As @busybear suggested, we can use the Patch Collection to plot the circles of different colours. This implementation works perfectly for the defined set of points. However, this was only done for 5 test data points. For more data points, we repeat the colours defined or there are no circles plotted.

Is there a way to also define a suitable colour spectrum/ colour table (Maybe with hex values or an rgb table with varying intensity) so we have varying colours for the circles? Circles with Different Colours

xx = np.linspace(0,10,10)
yy = np.linspace(30,60,10)
zz = np.vstack((xx,yy)).T
fig = plt.figure(1, figsize=(12, 5))
ax = fig.add_subplot(111, aspect='equal')
plt.plot(xx,yy,zorder=2)
plt.scatter(xx,yy,zorder=1)
circles = []
for i, c in zip(range(0,len(xx)), 'rgbmkrgbmk'):
    circles.append(plt.Circle(zz[i], 3, color=c, fill=True))
coll = PatchCollection(circles, match_original=True, zorder=0)
ax.add_collection(coll)
plt.autoscale()
plt.show()

New output (With Scatterplot method):

Circle cropped at corner

surajr
  • 85
  • 1
  • 8

1 Answers1

2

You set each circle's color to green with color='g', so you only need to change this to change their colors. You can set the order of your objects with zorder as you did with the scatter plot portion. With circles, you can use PatchCollection as in this answer. Here's what that might look like:

from matplotlib.collections import PatchCollection

...

circles = []
cmap = plt.cm.get_cmap('jet')
max_i = len(xx) - 1
for i in range(len(xx)):
    circles.append(plt.Circle(zz[i], 3, color=cmap(i/max_i), fill=True))
coll = PatchCollection(circles, match_original=True, zorder=0)
ax.add_collection(coll)

However, you could also do this all with just a scatterplot using the edges with each point:

plt.plot(xx, yy,'o-')
plt.scatter(xx, yy, s=[5000, 2000, 6000, 9000, 1000], c=range(len(xx)), cmap='jet')

enter image description here

busybear
  • 10,194
  • 1
  • 25
  • 42
  • While the Scatterplot technique is certainly convenient, unfortunately, it would not be useful for my particular application since I would need to plot the circles with varying radii and there would be more than 5 points, so would require more colour range – surajr Oct 15 '19 at 20:18
  • @surajr There is an easy way to create a N equally spaced color range. I'll look it up a bit later when I have my scripts on hand. – Mathieu Oct 15 '19 at 20:37
  • @surajr ot trying to force the scatter plot on you, but you could still use scatter plot. I've updated my post. – busybear Oct 15 '19 at 21:32
  • @busybear - Perfect, that fits exactly what I wanted! Thank you! – surajr Oct 16 '19 at 00:34
  • @busybear - Is there a way to scale the plot such that all the displayed circles fit correctly within the plot as in your plot? I find that for large radii, the first and last circles are slightly cropped by the edges even though I have defined a large figure size. Have uploaded a diagram for the same. – surajr Oct 16 '19 at 18:26
  • @busybear - Just a heads-up, in your updated code for Patch Collection, mc is not defined and there is an error when the code is run for the color attribute (Can't divide a tuple with an integer) – surajr Oct 16 '19 at 19:11
  • You can expand your x and y limits using `ax.set_xlim` and `ax.set_ylim`. That's probably easiest. I updated the post to include the import for PatchCollection. I'm not sure why you would get that error. Runs fine for me. – busybear Oct 16 '19 at 22:16