3

I have five sets of x,y data I'd like to plot in a single plt.plot() command by unwrapping the first dimension only of my array (of shape (5,2,500). If I try:

plt.plot(*arr)

I get the error

ValueError: third arg must be a format string

but if I plot by sending the x,y pairs separately, it works. e.g. for three lines:

plt.plot(arr[0][0], arr[0][1], arr[1][0], arr[1][1], arr[2][0], arr[2][1])

How can I unpack the first dimension only into the argument list for pt.plot?

1
  • 1
    I don't have time to write a complete answer right now. So i add this here, maybe someone else can make a complete answer from it. This seems to do what you're looking for plt.plot(arr[:,0,:].T, arr[:,1,:].T) Commented Jan 12, 2016 at 14:17

1 Answer 1

2
plt.plot(*arr)

is equivalent to

plt.plot(arr[0], arr[1], arr[2], arr[3], arr[4])

That's why it does not work.

As @M4rtini wrote in their comment, you can do plt.plot(arr[:,0,:].T, arr[:,1,:].T).

plt.plot(X, Y) creates a separate plot for each column in X and Y. Thus, arr[:, 0] and arr[:, 1] extract blocks of x and y coordinates, and .T transposes the blocks so that the first dimension goes into the columns.

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

Comments

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.