## matplotlib-users

 [Matplotlib-users] Drawing filled circles (discs) From: sidimok - 2007-09-15 08:25:13 ```Hi all! I'm using matplotlib to draw (from matplotlib.Patches import Circle) filled circles (disks) from a formatted data file, and would give each disk a color relative to a variable, as done by the "scatter" function. Any idea how to handle this, please? Thank's. -- View this message in context: http://www.nabble.com/Drawing-filled-circles-%28discs%29-tf4441651.html#a12672676 Sent from the matplotlib - users mailing list archive at Nabble.com. ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: - 2007-09-15 11:46:39 Attachments: text/plain text/plain text/plain
 Re: [Matplotlib-users] Drawing filled circles (discs) From: sidimok - 2007-09-19 12:04:30 ``` Jouni K. Sepp=C3=A4nen wrote: >=20 > sidimok writes: >=20 >> I'm using matplotlib to draw (from matplotlib.Patches import Circle) >> filled >> circles (disks) from a formatted data file, and would give each disk a >> color relative to a variable, as done by the "scatter" function. >=20 > Here's one way to do it: >=20 >=20 > #!/usr/bin/env python >=20 > import matplotlib > from matplotlib.patches import Circle > import pylab >=20 > def myscatter(ax, colormap, x, y, radii, colors): > for x1,y1,r,c in zip(x, y, radii, colormap(colors)): > ax.add_patch(Circle((x1,y1), r, fc=3Dc)) >=20 > fig=3Dpylab.figure() > ax=3Dfig.add_subplot(111) >=20 > myscatter(ax, matplotlib.cm.jet,=20 > pylab.rand(20), pylab.rand(20), 0.1*pylab.rand(20), > pylab.rand(20)) >=20 > ax.axis('equal') > pylab.show() >=20 >=20 >=20 >=20 Hi! It works just fine, however the colorbar I'm getting with the following commands is very tiny. norm =3D matplotlib.colors.Normalize(vmin=3Dx[0], vmax=3Dx[-1]) # just an e= xample cb =3D matplotlib.colorbar.ColorbarBase(ax, cmap=3Dmatplotlib.cm.jet, norm= =3Dnorm) Any idea? Thanks guys. --=20 View this message in context: http://www.nabble.com/Drawing-filled-circles-= %28discs%29-tf4441651.html#a12776069 Sent from the matplotlib - users mailing list archive at Nabble.com. ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: - 2007-09-19 13:27:46 ```sidimok writes: > It works just fine, however the colorbar I'm getting with the following > commands is very tiny. > > norm = matplotlib.colors.Normalize(vmin=x[0], vmax=x[-1]) # just an example > cb = matplotlib.colorbar.ColorbarBase(ax, cmap=matplotlib.cm.jet, norm=norm) It would help to give a complete example. ColorbarBase itself does not create a colorbar but is useful for drawing a colorbar in existing axes. -- Jouni K. Seppänen http://www.iki.fi/jks ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: sidimok - 2007-09-19 13:46:39 ``` Jouni K. Sepp=C3=A4nen wrote: >=20 >=20 > It would help to give a complete example. ColorbarBase itself does not > create a colorbar but is useful for drawing a colorbar in existing axes. >=20 >=20 Well, let's take the one you've proposed the last time. How than can i put = a colorbar beside the plot? #!/usr/bin/env python=20 import matplotlib=20 from matplotlib.patches import Circle=20 import pylab=20 def myscatter(ax, colormap, x, y, radii, colors):=20 for x1,y1,r,c in zip(x, y, radii, colormap(colors)):=20 ax.add_patch(Circle((x1,y1), r, fc=3Dc))=20 fig=3Dpylab.figure()=20 ax=3Dfig.add_subplot(111)=20 myscatter(ax, matplotlib.cm.jet,=20 pylab.rand(20), pylab.rand(20), 0.1*pylab.rand(20), pylab.rand(20))=20 ax.axis('equal')=20 pylab.show() --=20 View this message in context: http://www.nabble.com/Drawing-filled-circles-= %28discs%29-tf4441651.html#a12777776 Sent from the matplotlib - users mailing list archive at Nabble.com. ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: - 2007-09-19 15:06:20 ```sidimok writes: > Well, let's take the one you've proposed the last time. How than can i put a > colorbar beside the plot? Add in the imports from matplotlib.colorbar import ColorbarBase, make_axes and change the myscatter function to def myscatter(ax, colormap, x, y, radii, colors): for x1,y1,r,c in zip(x, y, radii, colormap(colors)): ax.add_patch(Circle((x1,y1), r, fc=c)) cax, _ = make_axes(ax) ColorbarBase(cax, cmap=colormap) A better solution is probably to implement a CircleCollection similarly to the other collections. -- Jouni K. Seppänen http://www.iki.fi/jks ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: Mika, David P (GE, Research) - 2007-09-19 18:43:43 ```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=20 fig=3Dpylab.figure() ax=3Dfig.add_subplot(111)=20 N =3D 20 x =3D pylab.rand(N) y =3D pylab.rand(N) radii =3D 0.1*pylab.rand(N) colors =3D 100*pylab.rand(N) verts =3D [] for x1,y1,r in zip(x, y, radii): circle =3D CirclePolygon((x1,y1), r) verts.append(circle.get_verts()) =20 p =3D PolyCollection(verts, cmap=3Dmatplotlib.cm.jet) p.set_array(pylab.array(colors)) ax.add_patch(p) pylab.colorbar(p) ax.axis('equal') pylab.show() -----Original Message----- From: matplotlib-users-bounces@... = [mailto:matplotlib-users-bounces@...] On Behalf Of = Jouni K. Sepp=E4nen Sent: Wednesday, September 19, 2007 11:03 AM To: matplotlib-users@... Subject: Re: [Matplotlib-users] Drawing filled circles (discs) sidimok writes: > Well, let's take the one you've proposed the last time. How than can i = > put a colorbar beside the plot? Add in the imports from matplotlib.colorbar import ColorbarBase, make_axes and change the myscatter function to def myscatter(ax, colormap, x, y, radii, colors): for x1,y1,r,c in zip(x, y, radii, colormap(colors)): ax.add_patch(Circle((x1,y1), r, fc=3Dc)) cax, _ =3D make_axes(ax) ColorbarBase(cax, cmap=3Dcolormap) A better solution is probably to implement a CircleCollection similarly = to the other collections. -- Jouni K. Sepp=E4nen http://www.iki.fi/jks -------------------------------------------------------------------------= This SF.net email is sponsored by: Microsoft Defy all challenges. = Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Matplotlib-users mailing list Matplotlib-users@... https://lists.sourceforge.net/lists/listinfo/matplotlib-users ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: Mika, David P (GE, Research) - 2007-09-20 12:38:02 ```In the solution I gave, CirclePolygon has a resolution argument for number of vertices to approximate the circle (default=3D20). You could increase that value to some more appropriate level: import matplotlib from matplotlib.patches import CirclePolygon from matplotlib.collections import PolyCollection import pylab=20 fig=3Dpylab.figure() ax=3Dfig.add_subplot(111)=20 resolution =3D 50 # the number of vertices=20 N =3D 20 x =3D pylab.rand(N) y =3D pylab.rand(N) radii =3D 0.1*pylab.rand(N) colors =3D 100*pylab.rand(N) verts =3D [] for x1,y1,r in zip(x, y, radii): circle =3D CirclePolygon((x1,y1), r, resolution) verts.append(circle.get_verts()) =20 p =3D PolyCollection(verts, cmap=3Dmatplotlib.cm.jet) p.set_array(pylab.array(colors)) ax.add_patch(p) pylab.colorbar(p) ax.axis('equal') pylab.show() -----Original Message----- From: matplotlib-users-bounces@... [mailto:matplotlib-users-bounces@...] On Behalf Of sidimok Sent: Thursday, September 20, 2007 4:59 AM To: matplotlib-users@... Subject: Re: [Matplotlib-users] Drawing filled circles (discs) Mika, David P (GE, Research) wrote: >=20 > How about this solution? I'm a complete newbe, but this seems to do=20 > 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=20 > PolyCollection. Enjoy, Dave >=20 >=20 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. http://www.nabble.com/file/p12793350/image.png -- View this message in context: http://www.nabble.com/Drawing-filled-circles-%28discs%29-tf4441651.html# a12793350 Sent from the matplotlib - users mailing list archive at Nabble.com. ------------------------------------------------------------------------ - This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Matplotlib-users mailing list Matplotlib-users@... https://lists.sourceforge.net/lists/listinfo/matplotlib-users ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: - 2007-09-20 19:14:50 ```sidimok writes: > 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. As he said, increasing the number of vertices could be enough, depending on your exact needs. If you zoom in on polygons, you will of course eventually see the difference. > May I ask how to create a circleCollection as Jouni "The Expert" > proposed? I meant that you could read through collections.py and implement a CircleCollection along the lines of the other collections there. I'm not quite sure what exactly this entails [so I'm not expert enough to answer your question :-)]. At least it would mean a new method for backends, although one that you could implement once in backend_bases. (Now that I look at collections.py, the base class Collection has a get_verts method that derived classes should override and that other parts of matplotlib call, so perhaps collections of non-polygons would require more extensive changes than just adding a new subclass.) The advantage would be speed of rendering in case you draw lots of circles, so if speed is not a problem, don't worry about this. -- Jouni K. Seppänen http://www.iki.fi/jks ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: sidimok - 2008-06-15 12:48:43 ``` 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) ax.axis('equal') pylab.show() - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Hi everyone, 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? -- View this message in context: http://www.nabble.com/Drawing-filled-circles-%28discs%29-tp12672676p17849649.html Sent from the matplotlib - users mailing list archive at Nabble.com. ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: John Hunter - 2008-06-17 01:22:32 ```On Sun, Jun 15, 2008 at 7:48 AM, sidimok 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' << > > 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... Michael, do you think it is a good idea to add a get_verts compatability method to the patches where it makes sense? JDH ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: Eric Firing - 2008-06-17 01:30:36 ```John Hunter wrote: > On Sun, Jun 15, 2008 at 7:48 AM, sidimok 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' << >> 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 > > Michael, do you think it is a good idea to add a get_verts > compatability method to the patches where it makes sense? > > JDH > > ------------------------------------------------------------------------- > Check out the new SourceForge.net Marketplace. > It's the best place to buy or sell services for > just about anything Open Source. > http://sourceforge.net/services/buy/index.php > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: sidimok - 2008-06-17 06:35:39 ``` > 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. Isn't it better to use "Circle" new collection? -- View this message in context: http://www.nabble.com/Drawing-filled-circles-%28discs%29-tp12672676p17879009.html Sent from the matplotlib - users mailing list archive at Nabble.com. ```
 Re: [Matplotlib-users] Drawing filled circles (discs) From: sidimok - 2007-09-20 08:58:49 ``` Mika, David P (GE, Research) wrote: > > 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. http://www.nabble.com/file/p12793350/image.png -- View this message in context: http://www.nabble.com/Drawing-filled-circles-%28discs%29-tf4441651.html#a12793350 Sent from the matplotlib - users mailing list archive at Nabble.com. ```