Plotting filled circles

I'm a new user of matplotlib (athough I have been using both Matlab and
Python for years).

I have an application where I need to display data as a set of filled
circles. The centre and the radius of the circles are both specified in
data coordinates. The data points have an additional scalar attribute,
which is displayed using a pseudo-colour mapping.

I've hacked something together where the circles are approximated by
polygons using either the axis fill method or the pylab fill function. I
select the colour by calling the get_rgba method of a ScalarMappable
object. In the following code snippet c is a tuple containg the x and y
coords of the centre of the circle, the radius, and a scalar "value"

theta = arange(numSegs+1) * 2.0 * math.pi / numSegs
cos_theta = cos(theta)
sin_theta = sin(theta)
for c in cart:
    x = c[1] + c[3] * cos_theta
    y = c[2] + c[3] * sin_theta
    v = c[4]
    fill(x, y, facecolor=mapper.to_rgba(v), linewidth=0.5)

It all works. However, I saw in the API documentation (and the source)
that there is a Circle object in patch. I was hoping that using this
rather than polygons would give better quality output and possibly
smaller files. Now I can instantiate it

        circle = Circle((x,y), c[3], facecolor=cmapper.to_rgba(v))

but can't work out what to do with it! I've tried

        ax.add_patch( circle )

and also

        trans = blend_xy_sep_transform( ax.transAxes, ax.transData )
        circle.set_transform( trans )
        ax.add_patch( circle )

Neither work. Any ideas?

Regards,

Andrew

Hi,

being a newbie myself, .. I don't know if I'm misguiding you. But do wonder if you looked at the scatter command
see scatter_demo2.py on
http://matplotlib.sourceforge.net/screenshots.html for an example

It *seems* like it already does what you are trying to do?, maybe I'm missing something in my understanding.

Steve

Andrew McLean wrote:

···

I'm a new user of matplotlib (athough I have been using both Matlab and
Python for years).

I have an application where I need to display data as a set of filled
circles. The centre and the radius of the circles are both specified in
data coordinates. The data points have an additional scalar attribute,
which is displayed using a pseudo-colour mapping.

I've hacked something together where the circles are approximated by
polygons using either the axis fill method or the pylab fill function. I
select the colour by calling the get_rgba method of a ScalarMappable
object. In the following code snippet c is a tuple containg the x and y
coords of the centre of the circle, the radius, and a scalar "value"

theta = arange(numSegs+1) * 2.0 * math.pi / numSegs
cos_theta = cos(theta)
sin_theta = sin(theta)
for c in cart:
    x = c[1] + c[3] * cos_theta
    y = c[2] + c[3] * sin_theta
    v = c[4]
    fill(x, y, facecolor=mapper.to_rgba(v), linewidth=0.5)

It all works. However, I saw in the API documentation (and the source)
that there is a Circle object in patch. I was hoping that using this
rather than polygons would give better quality output and possibly
smaller files. Now I can instantiate it

        circle = Circle((x,y), c[3], facecolor=cmapper.to_rgba(v))

but can't work out what to do with it! I've tried

        ax.add_patch( circle )

and also

        trans = blend_xy_sep_transform( ax.transAxes, ax.transData )
        circle.set_transform( trans )
        ax.add_patch( circle )

Neither work. Any ideas?

Regards,

Andrew

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Steve,

I guess I should have highlighted the difference. For my application I need the circle radii to be given in data coordinates. For the curious, I'm plotting something very similar to Dorling Cartograms. I did look in the source of "scatter" for clues, but it uses a 20 sided polygon to approximate a circle. It looks like scatter_classic did what I wanted, but that's been removed :-(.

- Andrew

Stephen George wrote:

···

It *seems* like it already does what you are trying to do?, maybe I'm missing something in my understanding.

Steve

Andrew McLean wrote:

I'm a new user of matplotlib (athough I have been using both Matlab and
Python for years).

I have an application where I need to display data as a set of filled
circles. The centre and the radius of the circles are both specified in
data coordinates. The data points have an additional scalar attribute,
which is displayed using a pseudo-colour mapping.

I've hacked something together where the circles are approximated by
polygons using either the axis fill method or the pylab fill function. I
select the colour by calling the get_rgba method of a ScalarMappable
object. In the following code snippet c is a tuple containg the x and y
coords of the centre of the circle, the radius, and a scalar "value"

theta = arange(numSegs+1) * 2.0 * math.pi / numSegs
cos_theta = cos(theta)
sin_theta = sin(theta)
for c in cart:
    x = c[1] + c[3] * cos_theta
    y = c[2] + c[3] * sin_theta
    v = c[4]
    fill(x, y, facecolor=mapper.to_rgba(v), linewidth=0.5)

It all works. However, I saw in the API documentation (and the source)
that there is a Circle object in patch. I was hoping that using this
rather than polygons would give better quality output and possibly
smaller files. Now I can instantiate it

        circle = Circle((x,y), c[3], facecolor=cmapper.to_rgba(v))

but can't work out what to do with it! I've tried

        ax.add_patch( circle )

and also

        trans = blend_xy_sep_transform( ax.transAxes, ax.transData )
        circle.set_transform( trans )
        ax.add_patch( circle )

Neither work. Any ideas?

Regards,

Andrew

Andrew McLean wrote:

I'm a new user of matplotlib (athough I have been using both Matlab and
Python for years).

I have an application where I need to display data as a set of filled
circles. The centre and the radius of the circles are both specified in
data coordinates. The data points have an additional scalar attribute,
which is displayed using a pseudo-colour mapping.

I've hacked something together where the circles are approximated by
polygons using either the axis fill method or the pylab fill function. I
select the colour by calling the get_rgba method of a ScalarMappable
object. In the following code snippet c is a tuple containg the x and y
coords of the centre of the circle, the radius, and a scalar "value"

theta = arange(numSegs+1) * 2.0 * math.pi / numSegs
cos_theta = cos(theta)
sin_theta = sin(theta)
for c in cart:
    x = c[1] + c[3] * cos_theta
    y = c[2] + c[3] * sin_theta
    v = c[4]
    fill(x, y, facecolor=mapper.to_rgba(v), linewidth=0.5)

It all works. However, I saw in the API documentation (and the source)
that there is a Circle object in patch. I was hoping that using this
rather than polygons would give better quality output and possibly
smaller files. Now I can instantiate it

        circle = Circle((x,y), c[3], facecolor=cmapper.to_rgba(v))

but can't work out what to do with it! I've tried

        ax.add_patch( circle )

This works for me in the sense that I can put circles or ellipses on a plot. Note that if you are trying to do this interactively you need to explicitly call draw() or draw_if_interactive() (pylab functions) after the call to add_patch, because the axis methods differ from pylab functions in that the latter automatically call draw_if_interactive() but the former do not.

The circle in data coordinates is a circle, however, only if you use a 1:1 aspect ratio. Does this suit your needs? If so you can get it with the pylab axis('equal') or axis('scaled') command. Or do you really want to specify the radius of the circle in x-data units or in y-data units? (I have no idea what a Dorling Cartogram is.)

and also

        trans = blend_xy_sep_transform( ax.transAxes, ax.transData )

This doesn't sound like what you want at all; it is used when you want to specify the x-coordinate in normalized axes units and the y-coordinate in data units.

Eric

···

        circle.set_transform( trans )
        ax.add_patch( circle )

Neither work. Any ideas?

Regards,

Andrew

Eric Firing wrote:

Andrew McLean wrote:

It all works. However, I saw in the API documentation (and the source)
that there is a Circle object in patch. I was hoping that using this
rather than polygons would give better quality output and possibly
smaller files. Now I can instantiate it

        circle = Circle((x,y), c[3], facecolor=cmapper.to_rgba(v))

but can't work out what to do with it! I've tried

        ax.add_patch( circle )

This works for me in the sense that I can put circles or ellipses on a plot. Note that if you are trying to do this interactively you need to explicitly call draw() or draw_if_interactive() (pylab functions) after the call to add_patch, because the axis methods differ from pylab functions in that the latter automatically call draw_if_interactive() but the former do not.

That was useful. Thanks. Turns out the problem was that I wasn't setting the axis limits. I added:

    axis([xmin, xmax, ymin, ymax])

and it all worked.

The circle in data coordinates is a circle, however, only if you use a 1:1 aspect ratio. Does this suit your needs? If so you can get it with the pylab axis('equal') or axis('scaled') command. Or do you really want to specify the radius of the circle in x-data units or in y-data units? (I have no idea what a Dorling Cartogram is.)

I was using axis('equal'). And yes this is what I want ;-). The x and y data units should map to the same physical scale and the radius is in those units.

Dorling cartograms described here (with an example near the bottom of the page):

http://www.ncgia.ucsb.edu/projects/Cartogram_Central/types.html

Regards,

Andrew

Andrew McLean wrote:
[...]

Dorling cartograms described here (with an example near the bottom of the page):

http://www.ncgia.ucsb.edu/projects/Cartogram_Central/types.html

Thanks for the link.

Eric

···

Regards,

Andrew