david_doji david_doji - 1 year ago 71
Python Question

plot arrays by row with matplotlib

I have two numpy arrays (a and b) with shape (16, 850) each. I'm displaying them row by row, e.g.

plt.plot(a[0], b[0])
plt.plot(a[1], b[1])
plt.plot(a[2], b[2])

Should I have to use a for loop to do it in a more pythonic way?

Answer Source

You can pass a multi-dimensional array to plot and each column will be created as a separate plot object. We transpose both inputs so that it will plot each row separately.

a = np.random.rand(16, 850)
b = np.random.rand(16, 850)

plt.plot(a.T, b.T)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download