[Numpy-discussion] problem with array/ndarray/list parameter to function

Benjamin Root ben.root@ou....
Thu Mar 10 10:08:28 CST 2011


On Thu, Mar 10, 2011 at 4:04 AM, Mic J <michael.cognacc@gmail.com> wrote:

> Hi i have a problem with passing a parameter to a function
>
>
> xtickvals = np.arange(0, total_project_days+5,  5)
>
> its for setting ticks on an axis, the x-axis
>
> Ex1: This works
> xticks = pylab.setp(ax2,
> xticklabels=['0','10','20','30','40','50','60','70','80','90','100'])
>
> Ex2: This doesnt work
> xticklabels=['0', '5', '10', '15', '20', '25', '30', '35', '40', '45',
> '50',
> '55', '60', '65']
> xticks = pylab.setp(ax2, xticklabels)
>
> I want to calculate how many tick is needed on that axis and then pass it
> to
> function.
> So that why i need something like the second example (or another way?)
>

Mic,

The problem is that there is a difference between "positional" arguments and
"keyword" arguments.  In the first example, the second argument is passed as
a keyword argument (due to the equal sign).  This allows python to know
which argument (by name) you want that value to be associated with.
However, in the second example, the second argument is given as a positional
argument (due to the lack of an equal sign).  Python knows which argument
(by position) you want that value associated with.

So, to make your second example work, just do this:

xticklabels=['0', '5', '10', '15', '20', '25', '30', '35', '40', '45', '50',
'55', '60', '65']
xticks = pylab.setp(ax2, xticklabels=xticklabels)

That's it!

I hope this helps!
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110310/06ab79b9/attachment-0001.html 


More information about the NumPy-Discussion mailing list