Search code examples
pythonnumpymatplotlibplotfrequency

Plot excess points on x axis in python


I am looking to plot data captured at 240 hz(x axis) vs data captured at 60hz(y axis). The x axis data is 4 times that of y axis and I would like 4 points on x axis to be plotted for 1 point on y axis, so that the result graph looks like a step. My list: Y axis: [0.0, 0.001, 0.003, 0,2, 0.4, 0.5, 0.7, 0.88, 0.9, 1.0] X Axis: np.arange(1, 40) # numpy

Any ideas how to club the 4 excess points into one in the graph?


Solution

  • You can use numpy.repeat to duplicate each data point in your series as many times as you want. For your specific example:

    from matplotlib import pyplot as plt
    import numpy as np
    
    fig, ax = plt.subplots()
    
    X = np.arange(1,41)
    Y = np.array([0.0, 0.001, 0.003, 0.2, 0.4, 0.5, 0.7, 0.88, 0.9, 1.0])
    Y2 = np.repeat(Y,4)
    print(Y2)
    
    ax.plot(X,Y2)
    
    plt.show()
    

    Gives the following output for Y2:

    [0.    0.    0.    0.    0.001 0.001 0.001 0.001 0.003 0.003 0.003 0.003
     0.2   0.2   0.2   0.2   0.4   0.4   0.4   0.4   0.5   0.5   0.5   0.5
     0.7   0.7   0.7   0.7   0.88  0.88  0.88  0.88  0.9   0.9   0.9   0.9
     1.    1.    1.    1.   ]
    

    And the following figure:

    adding points to Y

    You can also do the opposite with

    X2 = X[::4]
    ax.plot(X2, Y)
    

    In which case you get this figure:

    removing points from X