On Thu, Nov 27, 2008 at 11:21 AM, Mauro Cavalcanti <[EMAIL PROTECTED]> wrote:

> Then, in a checkbox event, I do the following:
>
> index = event.GetSelection()
> plot = self.plot_list[index]
> if self.FileList.IsChecked(index):
>       plot[0].set_visible(True)
> else:
>       plot[0].set_visible(False)
>
> This works quite well and points are properly toggled on/off the Basemap.
>
> But then I want to plot minimum spanning trees connecting the point
> datasets. For this I do:
>
> n = len(nodes)
> for i in range(n):
>       try:
>               t = edges[i,0]-1
>               u = edges[i,1]-1
>               x = [nodes[t,0], nodes[u,0]]
>               y = [nodes[t,1], nodes[u,1]]
>              self.plot_list.append(map.ax.plot(x,y,'-b'))
>       except:
>               continue
>
> where nodes is the array of point coordinates and edges are the
> from/to indexes of the coordinates, computed with Prim's algorithm.
> The lines between points (representing the minimum spanning tree) are
> displayed OK. But then my problem appears: I cannot find a way to turn
> the entire tree (which is composed of n-1 line segments) on and off of
> the map in the same way I do with the points (as shown above). What I
> would like is to store all line segments inside the for loop, and then
> show the tree at once, outside the loop; so I could use the same
> "plot[0].set_visible(True|False)" I use for the points.

Hi Mauro,

Yes, you should be able to use a collection for this quite easily.
One comment first.  You never want to try/except and catch all
exceptions w/o handling them in some way.  If you want to catch a
specific exception, fine, or of you want to catch all of them and log
them and then reraise, also fine, but there is not good use case for
catching them all and then continuing silently.

Now, on to collections.  In your example above, you would simply do::

    segments = []
    n = len(nodes)
    for i in range(n):
        t = edges[i,0]-1
        u = edges[i,1]-1
        xt, yt = nodes[t,0], nodes[t,1]   # assuming xt, yt are scalars here....
        xu, yu = nodes[u,0], nodes[u,1]
        segments.append( [ (xt,yt),  (xu,yu)  ] )

    collection = collections.LineCollection(segments)

and later::

    collection.set_visible(True|False)  # etc...

Hope this helps,

JDH

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to