How about this solution? I'm a complete newbe, but this seems to do the trick. I didn't see a CircleCollection so I used CirclePolygon to generate vertices for a circle; these I grab and toss into a PolyCollection. Enjoy, Dave
import matplotlib
from matplotlib.patches import CirclePolygon from matplotlib.collections import PolyCollection import pylab
fig=pylab.figure()
ax=fig.add_subplot(111)
N = 20
x = pylab.rand(N)
y = pylab.rand(N)
radii = 0.1*pylab.rand(N)
colors = 100*pylab.rand(N)
verts = []
for x1,y1,r in zip(x, y, radii):
circle = CirclePolygon((x1,y1), r)
verts.append(circle.get_verts())
p = PolyCollection(verts, cmap=matplotlib.cm.jet)
p.set_array(pylab.array(colors))
ax.add_patch(p)
pylab.colorbar(p)
How about this solution? I'm a complete newbe, but this seems to do the
trick. I didn't see a CircleCollection so I used CirclePolygon to
generate vertices for a circle; these I grab and toss into a
PolyCollection. Enjoy, Dave
Hi all!
Thank you very much indeed for the help, both solutions work like a charm.
However Dave's one gives rough cirlces, approximated by polygones, which is
not very accurate for my buisness. May I ask how to create a
circleCollection as Jouni "The Expert" proposed?
You can find below one of my plottings rendered by Jouni's first trick.
In the solution I gave, CirclePolygon has a resolution argument for
number of vertices to approximate the circle (default=20). You could
increase that value to some more appropriate level:
import matplotlib
from matplotlib.patches import CirclePolygon
from matplotlib.collections import PolyCollection
import pylab
fig=pylab.figure()
ax=fig.add_subplot(111)
resolution = 50 # the number of vertices
N = 20
x = pylab.rand(N)
y = pylab.rand(N)
radii = 0.1*pylab.rand(N)
colors = 100*pylab.rand(N)
verts = []
for x1,y1,r in zip(x, y, radii):
circle = CirclePolygon((x1,y1), r, resolution)
verts.append(circle.get_verts())
p = PolyCollection(verts, cmap=matplotlib.cm.jet)
p.set_array(pylab.array(colors))
ax.add_patch(p)
pylab.colorbar(p)
How about this solution? I'm a complete newbe, but this seems to do
the trick. I didn't see a CircleCollection so I used CirclePolygon to
generate vertices for a circle; these I grab and toss into a
PolyCollection. Enjoy, Dave
Hi all!
Thank you very much indeed for the help, both solutions work like a
charm.
However Dave's one gives rough cirlces, approximated by polygones, which
is not very accurate for my buisness. May I ask how to create a
circleCollection as Jouni "The Expert" proposed?
You can find below one of my plottings rendered by Jouni's first trick.
import matplotlib
from matplotlib.patches import CirclePolygon
from matplotlib.collections import PolyCollection
import pylab
fig=pylab.figure()
ax=fig.add_subplot(111)
resolution = 50 # the number of vertices
N = 20
x = pylab.rand(N)
y = pylab.rand(N)
radii = 0.1*pylab.rand(N)
colors = 100*pylab.rand(N)
verts = []
for x1,y1,r in zip(x, y, radii):
circle = CirclePolygon((x1,y1), r, resolution)
verts.append(circle.get_verts())
p = PolyCollection(verts, cmap=matplotlib.cm.jet)
p.set_array(pylab.array(colors))
ax.add_patch(p)
pylab.colorbar(p)
The new patches are more general since they work with general paths
and not just patches, but the snippet below is analogous::
verts = []
for x1,y1,r in zip(x, y, radii):
circle = CirclePolygon((x1,y1), r, resolution)
trans = circle.get_patch_transform()
path = circle.get_path()
transpath = path.transformed(trans)
verts.append(transpath.vertices
Note when you add the PolyCollection to the Axes, you should be use
ax.add_collection, not ax.add_patch::
p = PolyCollection(verts, cmap=matplotlib.cm.jet)
p.set_array(pylab.array(colors))
ax.add_collection(p)
But I'm guessing you could use a RegularPolyCollection here anyhow...
Michael, do you think it is a good idea to add a get_verts
compatability method to the patches where it makes sense?
JDH
···
On Sun, Jun 15, 2008 at 7:48 AM, sidimok <sidimok@...287...> wrote:
The code above was working for me as a charm, but since the new matlplotlib
flavor 0.98, I'm getting this error message:
AttributeError: 'CirclePolygon' object has no attribute 'get_verts' <<
The code above was working for me as a charm, but since the new matlplotlib
flavor 0.98, I'm getting this error message:
AttributeError: 'CirclePolygon' object has no attribute 'get_verts' <<
Any idea?
The new patches are more general since they work with general paths
and not just patches, but the snippet below is analogous::
verts = []
for x1,y1,r in zip(x, y, radii):
circle = CirclePolygon((x1,y1), r, resolution)
trans = circle.get_patch_transform()
path = circle.get_path()
transpath = path.transformed(trans)
verts.append(transpath.vertices
Note when you add the PolyCollection to the Axes, you should be use
ax.add_collection, not ax.add_patch::
p = PolyCollection(verts, cmap=matplotlib.cm.jet)
p.set_array(pylab.array(colors))
ax.add_collection(p)
But I'm guessing you could use a RegularPolyCollection here anyhow...
Or are you effectively doing a scatter plot? Could you use scatter?
Eric
···
On Sun, Jun 15, 2008 at 7:48 AM, sidimok <sidimok@...287...> wrote:
Michael, do you think it is a good idea to add a get_verts
compatability method to the patches where it makes sense?
Or are you effectively doing a scatter plot? Could you use scatter?
Eric
Yes, I've already tried doing the trick with a scatter plot, but since the
filling colors correspond to a "physical quantity" and the radius of the
scatters are the "real" circle radius, using RegularPolygons is more
straightforward.