Python: Plot array as function of time

56 Views Asked by At

I want to plot this exponential function:

T = np.random.exponential(3,50) as a function of time (Minutes 1-15) t = np.arange(0,15,1).

I get: x and y must have same first dimension...

Could you explain how it can be done properly? I'd like to understand.

1

There are 1 best solutions below

4
mozway On BEST ANSWER

T is not a function, it's an array. You must ensure that t and T have the same shape.

You should pass a size that is the same as the length of t to np.random.exponential:

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0, 15, 1)
T = np.random.exponential(3, size=len(t))

plt.plot(t, T)

Note that since T is random, there is no direct relationship between t and T.

Output:

enter image description here

Conversely, to scape t from T's shape, use np.linspace:

T = np.random.exponential(3, 50)
t = np.linspace(0, 15, num=len(T))
plt.plot(t, T)

Output:

numpy scaling array with linspace to plot