 # Single Point Stored Plot

Hello NG, I am facing a small problem for which I was

> unable to find a solution. Basically, I have to make a
> scatter plot of x, y coordinates (they represents oil
> wells positions) on a 2D map, but I have some constraints:

> 1) I would like to have each point as a separate "plot
> object", because the user may decide to hide/show only a
> particular well, or show/hide them all, or show/hide
> whatever combination of wells. 2) At the moment when the
> class that holds a single well position/status is created,
> the plot window may not be there (because is the user that
> decides when to open a new plot window).

> I have tried something like:

Hey Andrea, sorry for the delay. This is a neat problem and one that
collections were designed to solve. You probably want to use a
RegularPolyCollection rather than a PolyCollection (the latter is for
arbitrary polygons and you must compute all the vertices yourself).
Here is an example that allows you to add and delete markers -- it
inserts them with random positions and colors. You should be able to

import random
from matplotlib.colors import colorConverter
from matplotlib.collections import RegularPolyCollection
import matplotlib.cm as cm
from pylab import figure, show, nx

fig = figure()
ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
ax.set_title("Press 'a' to add a point, 'd' to delete one")
# a single point
offsets = [(0.5,0.5)]
facecolors = [cm.jet(0.5)]

collection = RegularPolyCollection(
fig.dpi,
numsides=5, # a pentagon
rotation=0,
sizes=(50,),
facecolors = facecolors,
edgecolors = (colorConverter.to_rgba('black'),),
linewidths = (1,),
offsets = offsets,
transOffset = ax.transData,
)

def onpress(event):
"""
press 'a' to add a random point from the collection, 'd' to delete one
"""
if event.key=='a':
x,y = nx.mlab.rand(2)
color = cm.jet(nx.mlab.rand())
offsets.append((x,y))
facecolors.append(color)
fig.canvas.draw()
elif event.key=='d':
N = len(offsets)
if N>0:
ind = random.randint(0,N-1)
offsets.pop(ind)
facecolors.pop(ind)
fig.canvas.draw()

fig.canvas.mpl_connect('key_press_event', onpress)

show()