How to get data in DateTime format from plot with Events in python?

前端 未结 1 1432
逝去的感伤
逝去的感伤 2021-01-19 11:03

I have data in iPython DataFrame like this

    Date                        X           Y           Z
0   2015-11-30 20:23:05.556     281.764900          


        
相关标签:
1条回答
  • 2021-01-19 11:33

    Matplotlib's date support pre-dates numpy's datetime support (and predates pandas entirely). Therefore, it uses its own internal date convention.

    To convert the "raw" number you're getting back to a datetime, use matplotlib.dates.num2date.

    As an example similar to what you're doing:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    import matplotlib.dates as mdates
    
    def on_click(event):
        if event.inaxes is not None:
            print mdates.num2date(event.xdata)
    
    t = pd.date_range('2015-11-01', '2016-01-06', freq='H')
    y = np.random.normal(0, 1, t.size).cumsum()
    
    df = pd.DataFrame({'Y':y}, index=t)
    
    fig, ax = plt.subplots()
    df.plot(ax=ax)
    fig.canvas.mpl_connect('button_press_event', on_click)
    plt.show()
    
    0 讨论(0)
提交回复
热议问题