david_doji david_doji - 6 months ago 24
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?


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)