From: Michael J Gruber <michaeljgruber+gmane@fa...>  20080421 14:36:14

Brendon Higgins venit, vidit, dixit 21.04.2008 06:36: > Stefan Schenk wrote (Fri, 18 Apr 2008): >> Am Freitag 18 April 2008 05:13 schrieb Brendon Higgins: >>> Thus my question: how can I change the ordering of the key without >>> changing the order in which the data is plotted? Is this even possible? >>> (Please tell me it is, somehow. I'm not afraid of digging into PyX code >>> if I have to. ;) ) >> I'm not sure if this is the most elegant way in PyX, but the following >> seems to work. >> >> [snip] > > Thanks, Stefan. It works, but unfortunately what you suggested isn't suitable > for where I want to put it. > > There doesn't seem to be an elegant way to solve this problem. So I patched > key.py to take a new parameter in the constructor: a list of numbers > indicating the zeroindexed order (itemorder). It will then use these to > reorder the plotitems list in the paint method. > > It *might* be a little rough around the edges, considering I'm not too > familiar with the code, but it's a pretty small change, and it seems to work. > I'd appreciate it if the developers might take a look. > > Peace, > Brendon > + iorder = [i for i in self.itemorder if 0 <= i and i < len(plotitems)] # ignore ordering indices that are out of range > + plotitems = [plotitems[i] for i in iorder] + plotitems[len(iorder):] # reorder the items It seems you require the itemorder parameter to have a certain form (being a permutation of the first n nonnegative integers) which you don't check: Imagine you have 2 plotitems and itemorder = [1]. This results in iorder=[1]. On the right hand side of the second assignment, you will get the following list: [ plotitems[1], plotitems[1] ] This is certainly not what you want. The above code works (gives a permutation of the list items) if, e.g., you require itemorder to be a permutation of the the first few consecutive nonneg. ints. You can check this by checking for set(iorder) == set(range(len(iorder))) after the consistency check that you have already. (This also invalidates input args like [1,1].) An alternative might be: plotitems = [plotitems[i] for i in iorder] + [plotitems[i] for i in set(range(len(plotitems)))set(iorder)] This would not require the above check. I'm not sure how expensive set operations are, though. Cheers, Michael 