4

I want to plot several curves out of one numpy array with one plotting object. The array has a form like:

position=np.array([[x11,x12,...,x1n],[y11,...,y1n],[x21,...,x2n],[y21,...],...])

It should do someting like the following code:

import matplotlib.pyplot as plt
import numpy as np

position=np.load("position.npy")

fig=plt.figure()
ax=fig.add_subplot(111,aspect='equal',autoscale_on=False)
p,=ax.plot(position[0],position[1],'y-',position[2],position[3],'y-',...)

but I need the last line to have a from like this:

p,=ax.plot(position)

I can't write down every position[i] in the plot command. Is there any way to do this, e.g. with a certain array shape or any additional arguments for the plot object? I need this to plot several trajectories in an animation where (xni,yni) would be the nth particle at time i.

Thanks a lot

2 Answers 2

3

You can unpack the list into a series of arguments. If the 'y-' isn't that important to you, this will work.

p, = ax.plot(*position)

if you want to add modifiers that apply to all the elements in your list use keyword arguments

p, = ax.plot(*position, linestyle = 'dashed', color = 'yellow')
Sign up to request clarification or add additional context in comments.

Comments

3

The docs for matplotlib.pyplot.plot(*args, **kwargs) say If x and/or y is 2-dimensional, then the corresponding columns will be plotted so you can slice the x and y values out of the position array:

x = position[::2,:].T 
y = position[1::2,:].T
p,=ax.plot(x, y,'y-')

1 Comment

Thanks @Hammer it does need a transpose. I've edited my answer.

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.