Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.figure()
plt.plot(a[0], b[0])
plt.plot(a[1], b[1]) 
plt.plot(a[2], b[2])
...
plt.show()

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

like image 356
Maxwell's Daemon Avatar asked Aug 14 '16 15:08

Maxwell's Daemon


People also ask

How do I plot an array in matplotlib?

From matplotlib we use the specific function i.e. pyplot(), which is used to plot two-dimensional data. Different functions used are explained below: np. arange(start, end): This function returns equally spaced values from the interval [start, end).


1 Answers

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)
plt.show()
like image 147
Suever Avatar answered Sep 20 '22 14:09

Suever