1

我找到了以下使用鼠标选择数据的示例,但是当我将 onpick 函数与极坐标图一起使用时,出现以下错误:

xdata = thisline.get_xdata()
AttributeError: 'PolarAxesSubplot' object has no attribute 'get_xdata'

使用鼠标获取 theta 和 r 的极坐标方法是什么?

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on points')

line, = ax.plot(np.random.rand(100), 'o', picker=5) # 5 points tolerance

def onpick(event):
    thisline = event.artist
    xdata = thisline.get_xdata()
    ydata = thisline.get_ydata()
    ind = event.ind
    print 'onpick points:', zip(xdata[ind], ydata[ind])

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()
4

1 回答 1

0

调整代码以打印极坐标图形,它对我有用:

import numpy as np
import matplotlib.pyplot as plt

line = plt.polar(2*np.pi*np.random.rand(100), np.random.rand(100),
                  'o', picker=5)[0] # 5 points tolerance
plt.gca().set_title('click on points')

def onpick(event):
    thisline = event.artist
    xdata = thisline.get_xdata()
    ydata = thisline.get_ydata()
    ind = event.ind
    print 'onpick points:', zip(xdata[ind], ydata[ind])

plt.gcf().canvas.mpl_connect('pick_event', onpick)

plt.show()
于 2013-08-12T07:27:06.873 回答