0

I am trying to avoid using "for" loops in some matplotlib plots. For this, I have tried the following code:

t=np.linspace(0,1,1000)
signals=[]

for k in range(8):  
    signals.append(np.sin(2*np.pi*t*k))
signals=np.array(signals)
    
units=np.array(['V', 'V', 'V', 'V', 'A', 'A', 'A', 'A'])
fig, axes = plt.subplots(8, 1,figsize=(10, 16))
# axes.plot(t,signals[units=="V",:])

The last commented line throws an error, because axes is a numpy array, which has no attribute "plot". I was trying to assign each axe to the plot of each signal.

How can I make this line with vectorization?

Thanks.

1 Answer 1

1

You cannot vectorize the creation of multiple subplots, but you can use a loop:

fig, axes = plt.subplots(signals.shape[0], 1, figsize=(8, signals.shape[0] * 2.5))

for i in range(signals.shape[0]):
    axes[i].plot(signals[i, ...])
    axes[i].set_ylabel('x(t) (unit: V)')
    ...

What you can do is plot everything into the same axes (simply use ax.plot(signals.T)).

Sign up to request clarification or add additional context in comments.

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.