2

I'm trying to wrap my head around the way positional and keyword arguments work in python, and, it seems, I'm failing rather miserably.

Given a function with a call signature matplotlib.pyplot.plot(*args,**kwargs), it can be called as

import matplotlib.pyplot as plt

x=[1,2,3]
y=[5,6,7]
plt.plot(x,y,'ro-')
plt.show()

Now, I'm trying to wrap it into something which I can call as mplot(x,y,'ro-',...) where ... are whatever arguments the original function was ready to accept. The following fails miserably, but I can't really figure how to fix it:

def mplot(x,y,fmt,*args,**kwargs):
   return plt.plot(x,y,fmt,*args,**kwargs)

mplot(x,y,'ro-')

Any pointers to a way out would be very much appreciated.

5
  • It should work (at least it does for me - Python 3.1)... any details on the error or what it did vs. what you expected? Commented Feb 19, 2011 at 17:08
  • 1
    I don't understand why you can't just call plot as it is. What's motivating this question? Commented Feb 19, 2011 at 17:11
  • 2
    This code looks actually fine - what is the real error? Commented Feb 19, 2011 at 17:16
  • 2
    "fails miserably" is useless as error description Commented Feb 19, 2011 at 17:17
  • Thanks to everybody. Indeed, that was a stupid question, with the real error in a different part of the code. The error's fixed, the lesson learned. Thanks again. Commented Feb 19, 2011 at 18:46

1 Answer 1

1

You need it this way:

def mplot(x,y,fmt,*args,**kwargs):
   #do stuff with x, y and fmt
   return plt.plot(*args,**kwargs)

I'm assuming that your intention is to consume the x, y and fmt in your mplot routine and then pass the remaining parameters to plt.plot.


I don't believe that this is actually what you want (I can see that plt.plot wants to receive x, y and fmt and so they should not be consumed). I had deleted this answer but since your posted code apparently works, I'll leave this visible for a little while and see if it provokes the real question to be revealed!

Sign up to request clarification or add additional context in comments.

9 Comments

@ David Heffernan: I'm actually going to accept your answer, since I saw it, and then figured the actual error in the actual code, which was way down the line, and not in fact related to this issue with the arguments.
@David Heffernan: As to your question what motivates all this fuss: given a set of points, fit them by a certain function, then plot the points and thea best-fit line. After copying this the 3rd time around, I thought I'd give this a name :-).
@Zhenya: Please let rest of us all so to know what was the original problem. It doesn't make really sense to just accept something and let rest of us wondering what's your real problem and solution for it. Thanks
@eat: If you're so curious, here it is: dtc=[('L1','float64'),('L2','float64'),('Tc','float64'),('dTc','float64'), ('yc','float64'),('dyc','float64'),("tag",np.str_,16)] crossings=np.zeros(len(pairs),dtype=dtc) using just a "string" in the numpy dtype generates segfault. Why is that I have no idea, but this (np.str_,16) seems to work as long as the length of the string is <16 characters.
@eat @Zhenya It would make a lot more sense for the real solution to be posted as an answer by Zhenya. I could then delete this and Zhenya could answer his/her own answer. The downvote was a bit pointless. I said I was going to delete this once it was resolved and I only undeleted the answer to try to help provoke some sense.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.