Jan*_*nar 6 python events matplotlib pandas
我在 iPython 中有DataFrame
这样的数据
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
Run Code Online (Sandbox Code Playgroud)
这些列dtypes
是:
Date datetime64[ns]
X float64
Y float64
Z float64
dtype: object
Run Code Online (Sandbox Code Playgroud)
我将其设置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)
Run Code Online (Sandbox Code Playgroud)
我想以Date
格式或String
格式从 x 轴获取数据,但在图表中单击后我只有这个
button=1, x=152, y=339, xdata=735932.856811, ydata=8.518828
Run Code Online (Sandbox Code Playgroud)
如何设置 xdata 中的输出String
?
Matplotlib 的日期支持早于 numpy 的日期时间支持(并且pandas
完全早于)。因此,它使用自己的内部日期约定。
要将您返回的“原始”数字转换为日期时间,请使用matplotlib.dates.num2date
。
作为与您正在做的类似的示例:
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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2231 次 |
最近记录: |