# Newbie question

Hi,
The matplotlib.collections.Collection documentation reads: "All properties in a collection must be sequences or scalars; if scalars, they will be converted to sequences. The property of the ith element of the collection is: prop[i % len(props)]". I had a look at the docstring documentation from ipython, but I didn't find out how the above is achieved (I am learning Python together with numpy, matplotlib, etc.). In my own code, how could I do something like this? If you point to a relevant location on the source code, that's alright. I also get confused sometimes because of the multiple (and sometimes interchangeable) ways of specifying arguments: sequences (list, tuples), numpy arrays, etc. I started using almost exclusively numpy arrays (probably due to my matlab background), but I am starting to mix a bit of everything now (depending on what "sources of inspiration" I use), so I wondered what a good guideline would be.

Thanks,

Jorge

Hi, The matplotlib.collections.Collection documentation reads: "All
properties in a collection must be sequences or scalars; if scalars,
they will be converted to sequences. The property of the ith element
of the collection is: prop[i % len(props)]". I had a look at the
docstring documentation from ipython, but I didn't find out how the
above is achieved (I am learning Python together with numpy,
matplotlib, etc.). In my own code, how could I do something like
this? If you point to a relevant location on the source code, that's

import matplotlib.cbook as cbook

def to_sequence(arg):
if cbook.is_iterable(arg):
return arg
return [arg]

Above is an example of how one can turn a scalar into a sequence (a list, in this case) if necessary.

alright. I also get confused sometimes because of the multiple (and
sometimes interchangeable) ways of specifying arguments: sequences
(list, tuples), numpy arrays, etc. I started using almost exclusively
numpy arrays (probably due to my matlab background), but I am
starting to mix a bit of everything now (depending on what "sources
of inspiration" I use), so I wondered what a good guideline would be.

Different types of sequence have different advantages and disadvantages. Tuples are immutable. Lists are much more flexible, and can be extended. ndarrays are fixed-size, but facilitate efficient computation.

If a function or method accepts any kind of sequence for a given argument, then probably the thing to do is give it whatever you have already, or whatever is most convenient to generate. Lists are a good default if the sequence has only a few elements and you are writing them out, rather than calculating them from some other sequence. In other words, if a function is flexible, then trust the function to do whatever conversions it needs internally; there is no particular advantage in doing the conversion yourself when you specify the argument.

Eric

···

jorgesmbox-ml@...1664... wrote:

When I enter this into ipython, I get:

In [67]: to_sequence(1)

···

----- Mensaje original ----

De: Eric Firing <efiring@...202...>

import matplotlib.cbook as cbook

def to_sequence(arg):
if cbook.is_iterable(arg):
return arg
return [arg]

Above is an example of how one can turn a scalar into a sequence (a list, in
this case) if necessary.

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)

/home/jscandal/sw/python/myimports.py in <module>()
----> 1
2
3
4
5

/home/jscandal/sw/python/myimports.py in to_sequence(arg)
1
----> 2
3
4
5

AttributeError: 'module' object has no attribute 'is_iterable'

I have matplotlib 0.98.5.2, and it seems is_iterable is not there.

are immutable. Lists are much more flexible, and can be extended. ndarrays are
fixed-size, but facilitate efficient computation.

If a function or method accepts any kind of sequence for a given argument, then
probably the thing to do is give it whatever you have already, or whatever is
most convenient to generate. Lists are a good default if the sequence has only
a few elements and you are writing them out, rather than calculating them from
some other sequence. In other words, if a function is flexible, then trust the
function to do whatever conversions it needs internally; there is no particular
advantage in doing the conversion yourself when you specify the argument.

Thanks for the hints, I guess I get a bit frustrated or lost sometimes. Trying to get work done while being at the bottom of the learning curve might not be the best situation. I have the feeling, though, that I had to switch this way or else I would never really do it at all. I've been wanting to play with python for a long time now, but never did anything more than hello world examples. Until now

Jorge

I goofed. It should be cbook.iterable(arg).

Eric

···

jorgesmbox-ml@...1664... wrote:

----- Mensaje original ----

De: Eric Firing <efiring@...202...>

import matplotlib.cbook as cbook

def to_sequence(arg):
if cbook.is_iterable(arg):