Andrew McLean | 2 Jul 01:19
Picon

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))

(Continue reading)

Stephen George | 2 Jul 01:39
Picon
Gravatar

Re: Plotting filled circles

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)
(Continue reading)

Andrew McLean | 2 Jul 02:09
Picon

Re: Plotting filled circles

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
(Continue reading)

Eric Firing | 2 Jul 09:20
Favicon
Gravatar

Re: Plotting filled circles

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))
(Continue reading)

Angel Lopez | 2 Jul 11:34
Picon

plot does not redraw

Hello,

I started using pylab-matplotlib some weeks ago. And it is really good.

I'm working now on windows (TkAgg).

I have written a small app that reads a file and plot it. User can
press a key to read another file, and plot it over. The problem is
that the I have to resize the plot window to see the new plotted line.

Here is the code:

import sys
import os
import tkFileDialog
import pylab

pylab.hold(1)
path='E:\\Nima'

def keyp(event):
    global path
    if event.key.lower()=='o':
        fin = tkFileDialog.askopenfilename(initialdir=path)
        path=os.path.split(fin)[0]
        plot2(fin)

def plot2(fin):
    global myplot
    f=file(fin, 'r')
(Continue reading)

Andrew McLean | 2 Jul 21:48
Picon

Re: Plotting filled circles

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 
(Continue reading)

Jonathan Griffitts | 2 Jul 22:12

legend() without args?

In matplotlib 0.90.1 the behavior of legend() seems to have changed.

Here's a test code fragment:
---------------------------
import pylab
import numpy

y=numpy.arange(-10,10)**2
print y
pylab.plot(y)
pylab.legend()
pylab.show()
---------------------------
Running on python 2.5.1, matplotlib 0.90.0 gives me a reasonable-looking
legend in upper right corner with label "line0".

matplotlib 0.90.1 gives an empty rectangular box, fairly large, right in
the middle of the plot.

Attempting to move the legend with loc=2 or whatever has no effect.
However, giving an explicit label ( pylab.legend(['line1']) ) makes it
start working the same as 0.90.0.

I started looking at the source but don't have time today to understand
how this is supposed to work.  Maybe later.

Is this the intended behavior?  The announcement for 0.90.1 says:
"Display only meaningful labels when calling legend() without args."
It's not clear to me what this means.

(Continue reading)

Eric Firing | 2 Jul 22:36
Favicon
Gravatar

Re: Plotting filled circles

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

-------------------------------------------------------------------------
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/
Matthias Michler | 2 Jul 23:50
Picon

Re: Prompt in MPL - NEVER MIND

Hello everybody,

first of all I hope my mails on the issue 'Prompt/TextBox/InputButton' don't 
become to many. 
But I had one more idea and I want to share it with you.
If you don't like my usage of lists as a pointer to variables, one could 
replace this in two steps: 
1) Adding a new method 'get_text' to get the actual label text 
(def get_text(self): 
     return self.label.get_text() )
2) In the example one would include a function to set one's variable to the 
evaluated label-text. Than one would add this function into the  
self.observers and so call it after every finished typing mode.

This new behaviour has the big advance that error-handling during evaluation 
of the text is a task for users and not to mpl. 

I hope I could explain my idea ...

best regards,
Matthias

On Wednesday 27 June 2007 18:19, Matthias Michler wrote:
> Hello everybody,
>
> in my last version I forgot to include a very useful function (it is
> comparable with the 'Button.on_clicked' function). I added it now. It
> allows the user to interact with other widgets ( I needed it to interact
> with the Silder) or his own program (e.g. updating external values or
> plots).
(Continue reading)

Christopher Barker | 2 Jul 23:57
Picon
Favicon

Re: export of a specific zone of a figure

I don't know how to do it with the MPL agg back-end, but I think you 
mentioned wx, and you can do it there instead. a wxImage can be 
constructed from a buffer object, then saved as a PNG. You may need to 
set the rgb and alpha portions separately. See the wxPython wiki and 
search for "Image".

Also:

>             matrix = []
>             buffer = self.get_renderer().tostring_argb()
>             l, h = self.GetSize()
>             for ligne in xrange(h):
>                 matrix.append([])
>                 for colonne in xrange(l):
>                     i = 4*(ligne*h + colonne)
>                     pixel = buffer[i:i+4]
>                     matrix[-1].append(pixel)

This is a very slow way to create the numpy array!

Option a: first create an empty array:

matrix = numpy.empty((l,h,4), numpy.byte)

then fill that in. but even better:

you can build the array directly from the buffer string:

matrix = numpy.fromstring(buffer, dtype=numpy.byte)
lotlib-users
(Continue reading)


Gmane