1

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.

4
  • 2
    So you are sure you want to plot 1360 lines? That seems a lot given that a normal screen only has some 1000 pixels in height. Commented Jan 4, 2019 at 14:05
  • 1
    I think you can do pyplot.plot(X, Y.T) (or without the transpose, pyplot.plot(X, Y), depending on how you stored your data) Commented Jan 4, 2019 at 14:06
  • @ImportanceOfBeingErnest Yes, they only change a bit and I want to see the space that's unoccupied. As an (unrelated) example google for "eye plot". Commented Jan 4, 2019 at 14:08
  • @TomdeGeus pyplot.plot(X,Y.T) did the trick! When I wrote down the shapes, that should have dawned on me. Commented Jan 4, 2019 at 14:10

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

Comments

2

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()

Comments

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.