tags:

views:

229

answers:

2

hi I want to draw graph but to give one list of the point them selves and not two lists of X's and Y's. something like that:

a=[[1,2],[3,3],[4,4],[5,2]] plt.plot(a,'ro')

(and not [1,3,4,5],[2,3,4,,2])

thanks

A: 

Write a helper function.

Here is a longish version, but I am sure there is a trick to compress it.

>>> def helper(lst):
    lst1, lst2 = [], []
    for el in lst:
        lst1.append(el[0])
        lst2.append(el[1])
    return lst1, lst2

>>> 
>>> helper([[1,2],[3,4],[5,6]])
([1, 3, 5], [2, 4, 6])
>>> 

Also add this helper:

def myplot(func, lst, flag):
    return func(helper(lst), flag)

And call it like so:

myplot(plt.plot, [[1,2],[3,4],[5,6]], 'ro')

Alternatively you could add a function to an already instantiated object.

Hamish Grubijan
+2  A: 

You can do something like this:

a=[[1,2],[3,3],[4,4],[5,2]]
plt.plot(*zip(*a))

Unfortunately, you can no longer pass 'ro'. You must pass marker and line style values as keyword parameters:

a=[[1,2],[3,3],[4,4],[5,2]]
plt.plot(*zip(*a), marker='o', color='r', ls='')

The trick I used is unpacking argument lists.

ianalis