如何从带有Python事件的情节中获取DateTime格式的数据? [英] How to get data in DateTime format from plot with Events in python?

查看:137
本文介绍了如何从带有Python事件的情节中获取DateTime格式的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPython DataFrame中有这样的数据

I have data in iPython DataFrame like this

    Date                        X           Y           Z
0   2015-11-30 20:23:05.556     281.764900  -43.895060  8.714666
1   2015-11-30 20:23:05.757     192.519990  -44.636436  1.720552
2   2015-11-30 20:23:05.958     149.030600  -45.098050  1.958352
3   2015-11-30 20:23:06.171     140.707600  -44.622448  1.510729
4   2015-11-30 20:23:06.366     139.154890  -45.154003  4.783974
5   2015-11-30 20:23:06.564     138.875140  -44.790306  2.266093

这些列的dtypes是这些

Date    datetime64[ns]
X              float64
Y              float64
Z              float64
dtype: object

我将button_press_event设置为我的fig

def onclick(event):
    print 'button=%d, x=%d, y=%d, xdata=%s, ydata=%f'%(
        event.button, event.x, event.y, event.xdata, event.ydata)
cid = fig.canvas.mpl_connect('button_press_event', onclick)

,我想从Date格式或String格式的x轴获取数据,但是在图形中单击后,我就拥有了

and I want to get data from x axes in Date format or String format, but after click in graph I have just this

button=1, x=152, y=339, xdata=735932.856811, ydata=8.518828

如何在String中设置来自xdata的输出?

How to set output from xdata in String?

推荐答案

Matplotlib的日期支持早于numpy的日期时间支持(并完全早于pandas).因此,它使用其自己的内部日期约定.

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

要将您要返回的原始"数字转换为日期时间,请使用 matplotlib.dates.num2date .

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()

这篇关于如何从带有Python事件的情节中获取DateTime格式的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆