Multiple data set plotting with matplotlib.pyplot.plot_date

后端 未结 1 391
半阙折子戏
半阙折子戏 2021-01-02 19:05

this might be really a simple question for most of you guys using matplotlib. Please help me out. I want to plot two array like [1,2,3,4] and [4,5,6,7] versus time in a same

相关标签:
1条回答
  • 2021-01-02 19:31

    To use plot date with multiple trends, it's easiest to call it multiple times. For example:

    import datetime
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    
    # Generate Data
    time = mdates.drange(datetime.datetime(2010, 1, 1), 
                         datetime.datetime(2011, 1, 1),
                         datetime.timedelta(days=10))
    y1 = np.cumsum(np.random.random(time.size) - 0.5)
    y2 = np.cumsum(np.random.random(time.size) - 0.5)
    
    # Plot things...
    fig = plt.figure()
    
    plt.plot_date(time, y1, 'b-')
    plt.plot_date(time, y2, 'g-')
    
    fig.autofmt_xdate()
    plt.show()
    

    enter image description here

    Alternately you can use a single plot (rather than plot_date) call and then call plt.gca().xaxis_date(), if you'd prefer. plot_date just calls plot and then ax.xaxis_date().

    0 讨论(0)
提交回复
热议问题