pythonarraysnumpyplotdimensions

Plot Python Array


I'd like to plot T as a function of t.

T = np.random.exponential(3,50)

t = np.arange(0,15,1).

Error: x and y must have same first dimension


Solution

  • 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