Plotting time in Python with Matplotlib

前端 未结 4 1648
忘掉有多难
忘掉有多难 2020-11-22 09:04

I have an array of timestamps in the format (HH:MM:SS.mmmmmm) and another array of floating point numbers, each corresponding to a value in the timestamp array.

Can

4条回答
  •  星月不相逢
    2020-11-22 09:49

    I had trouble with this using matplotlib version: 2.0.2. Running the example from above I got a centered stacked set of bubbles.

    graph with centered stack of bubbles

    I "fixed" the problem by adding another line:

    plt.plot([],[])
    

    The entire code snippet becomes:

    import datetime
    import random
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    
    
    # make up some data
    x = [datetime.datetime.now() + datetime.timedelta(minutes=i) for i in range(12)]
    y = [i+random.gauss(0,1) for i,_ in enumerate(x)]
    
    # plot
    plt.plot([],[])
    plt.scatter(x,y)
    
    # beautify the x-labels
    plt.gcf().autofmt_xdate()
    myFmt = mdates.DateFormatter('%H:%M')
    plt.gca().xaxis.set_major_formatter(myFmt)
    
    plt.show()
    plt.close()
    

    This produces an image with the bubbles distributed as desired.

    graph with bubbles distributed over time

提交回复
热议问题