pythonmatplotliboscilloscope

Python plot 3 variables data on same axis?


We conduct experiments, our oscilloscope gives all plots on same screen though each variables is different in magnitude. Is it possible to achive same in the python using the experimental data?

My present code and output:

import random 

x = [i for i in range(1,11,1)]
y1 = random.sample(range(100, 1000), 10)
y2 = random.sample(range(0, 10), 10)

plt.plot(x,y1,'-r')
plt.plot(x,y2,'-g')
plt.legend(['y1','y2'])
plt.show()

enter image description here


Solution

  • There is a pretty simple solution to that just use subplots

    import random
    import matplotlib.pyplot as plt
    x = [i for i in range(1,11,1)]
    y1 = random.sample(range(100, 1000), 10)
    y2 = random.sample(range(0, 10), 10)
    
    ax1 = plt.subplot(211)
    plt.plot(x,y1,'-r')
    ax2 = plt.subplot(212,sharex=ax1)
    plt.plot(x,y2,'-g')
    ax1.get_shared_x_axes().join(ax1, ax2)
    #make x axis on upper invisible
    plt.setp(ax1.get_xaxis(), visible=False)
    
    ax1.legend(['y1'])
    ax2.legend(['y2'])
    plt.show()
    

    Looks like this enter image description here

    You can remove the bottom-border from the upper subplot and the upper border from the lower subplot with this:

    ax1.spines['bottom'].set_visible(False)
    ax2.spines['top'].set_visible(False)
    

    GridSpec might help you to remove margins, however I gues there should be a simpler way to remove the distance between two subplots