Matthias Arras
Matthias Arras

Reputation: 740

Plot multiple lines with same X axis in efficient way

I have a numpy array Y of shape (1360, 1024) which contains aggregated 1360 data set that each are of length 1024. I have another array of shape (1024,) called X.

This is what Y[0:5] looks like (as example):

array([[13.72059917, 16.27633476, 18.49536324, ...,  0.81599081,
         0.99834043,  0.92653233],
       [13.42022991, 15.06573963, 17.45792198, ...,  0.85495144,
         0.75660354,  1.02977574],
       [13.6416111 , 16.03499603, 17.46924019, ...,  0.85070604,
         0.94057351,  0.87749392],
       [14.69120216, 16.85452461, 17.6070137 , ...,  0.86291492,
         0.99953759,  0.81989962],
       [13.57082653, 16.15143394, 17.55677032, ...,  0.93469822,
         0.96676576,  1.09142995]])

Now I want to plot all the 1360 Y data sets on top of each other. For all of them the x-axis is the same, i.e. X.

I know I can do this to plot multiple things:

pyplot.plot(X,Y[0],X,Y[1],X,Y[2])

but that looks like brute force. Also this could be solved with a loop, but not very elegant.

I tried a bit with list comprehension to make the X,Y[0]... automatically but failed.

Ideally I want a one-line solution and no loop.

Upvotes: 1

Views: 11319

Answers (2)

YoniChechik
YoniChechik

Reputation: 1537

plt.plot reads plots by columns. here's a complete example:

import numpy as np
import matplotlib.pyplot as plt

xa = np.array([1, 2, 3])  # shape (3,)
xb = np.array([[1],
               [2],
               [3]])  # shape (3,1)
xc = np.array([[1, 4],
               [2, 5],
               [3, 6]])  # shape (3,2)

ya = np.array([[1, 4],
               [2, 5],
               [3, 6]])  # shape (3,2)
yb = np.array([1, 2, 3])  # shape (3,)

plt.figure()
plt.plot(xa, ya)  # res- 2 lines: ((1,1), (2,2), (3,3)) & ((1,4), (2,5), (3,6))

plt.figure()
plt.plot(xb, ya)  # res- 2 lines: ((1,1), (2,2), (3,3)) & ((1,4), (2,5), (3,6))

plt.figure()
plt.plot(xc, ya)  # res- 2 lines: ((1,1), (2,2), (3,3)) & ((4,4), (5,5), (6,6))

plt.figure()
plt.plot(xc.T, ya.T)  # res- 3 lines: ((1,1), (4,4)) & ((2,2),(5,5)) & ((3,3), (6,6))

plt.figure()
plt.plot(xa, yb)  # res- 1 line: ((1,1), (2,2), (3,3))

plt.figure()
plt.plot(xb, yb)  # res- 1 line: ((1,1), (2,2), (3,3))

plt.figure()
plt.plot(xc, yb)  # res- 2 lines: ((1,1), (2,2), (3,3)) & ((4,1), (5,2), (6,3))

plt.show()

Upvotes: 2

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339130

You can supply a 2D array to plot(x,y). If x is of length n, y must be of shape (n,m) to plot m lines (one line per column).

import numpy as np
import matplotlib.pyplot as plt

Y = np.random.rand(5,7)
X = np.arange(7)

plt.plot(X, Y.T)

plt.show()

For a large number of columns, this is however inefficient. A more efficient way to produce this plot is to draw a single "line" via a LineCollection

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

Y = np.random.rand(5,7)
X = np.arange(7)

x = np.tile(X, Y.shape[0]).reshape(*Y.shape)
v = np.stack((x,Y), axis=-1)
c = LineCollection(v)

fig, ax = plt.subplots()
ax.add_collection(c)
ax.autoscale()
plt.show()

Upvotes: 4

Related Questions