当然可以。matplotlib文档有一些有用的代码(https://matplotlib.org/users/event_handling.html#object-picking),您可以在qt、tk、wx等中以交互方式实现这些代码。。。通过使用%matplotlib qt #tk, wx, inline#(for notebook)等。。。如果你运行这段代码,我认为它所做的与你所追求的非常接近(我使用的是tk。。。所以改成qt,我想它还能工作)。运行它,然后单击散点图上的一个点来触发事件“onpick”并显示该“点”历史:"""
compute the mean and stddev of 100 data sets and plot mean vs stddev.
When you click on one of the mu, sigma points, plot the raw data from
the dataset that generated the mean and stddev
"""
import numpy as np
import matplotlib.pyplot as plt
%matplotlib tk
X = np.random.rand(100, 1000)
xs = np.mean(X, axis=1)
ys = np.std(X, axis=1)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click on point to plot time series')
line, = ax.plot(xs, ys, 'o', picker=5) # 5 points tolerance
def onpick(event):
if event.artist!=line: return True
N = len(event.ind)
if not N: return True
figi = plt.figure()
for subplotnum, dataind in enumerate(event.ind):
ax = figi.add_subplot(N,1,subplotnum+1)
ax.plot(X[dataind])
ax.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
transform=ax.transAxes, va='top')
ax.set_ylim(-0.5, 1.5)
figi.show()
return True
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
我对qt没有太多的经验,但对于tk来说,你似乎可以把matplotlib图形发送到画布上,它们工作得很好——所以我假设qt中也可能有类似的东西,但是根据你的问题,你已经把它嵌入其中,并且只是在进行交互。在
头脑清醒了我遇到了一个问题,花了一段时间才弄清楚
我不知道在qt中是否相同(也许其他人可以证实)。。。但是只要有一个头部,您可能需要在绘图时显式地设置picker=True。同样,我对qt一无所知,但这里有一个以上的tk实现,但是在一个没有picker=True的定制gui上是不能交互的:
^{pr2}$