我正在开发一个应用程序,它可以通过单击第一个图中的数据点来生成第二个图。我正在使用事件来实现这一目标。
题:
我到目前为止的代码:
"""
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
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(211)
bx = fig.add_subplot(212)
# ax.set_title('click on point to plot time series')
# bx.set_title('click on point to plot time series')
line, = ax.plot(xs, ys, 'bs', picker=5) # 5 points tolerance
line1, = bx.plot(xs, ys, 'bo', 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)
bx = figi.add_subplot(N,1,subplotnum+1)
bx.plot(X[dataind])
bx.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
transform=ax.transAxes, va='top')
bx.set_ylim(-0.5, 1.5)
figi.show()
return True
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
通过单击第一个图上的数据点,将生成第二个图。
现在的期望是生成第三个绘图点击第二个绘图数据点。
I have researched on this, but I am not successful :-( :-(
如果我的方法有误,请建议任何其他选择。
import matplotlib.pyplot as plt
import numpy as np
# data source
data_bucket = {}
# l1: randn
# l2: sum(l1)
# l3: sum(l2)
# generate some 3 layer synthetic data
N = 1000
l1_count = 50
l2_count = 50
l3_count = 2
x = np.arange(N)
for j in range(l3_count):
l3 = []
for k in range(l2_count):
l2 = []
for m in range(l1_count):
l1 = data_bucket[(j, k, m)] = np.random.randn(N)
l2.append(np.sum(l1))
l2 = data_bucket[(j, k)] = np.asarray(l2)
l3.append(np.sum(l2))
data_bucket[(j, )] = np.asarary(l3)
# only hase to close over the data
def picker(event):
print(event.artist.get_gid())
print(event.ind)
# some function to generate next data key from gid + index
k = event.artist.get_gid() + tuple(event.ind)
try:
next_data = data_bucket[k]
except KeyError:
print("no generations of data left")
return
# make the previous generation of plot
fig, ax = plt.subplots()
# can use the key here to dispatch to different plotting functions
ax.plot(next_data, gid=k, picker=5, ls='-')
fig.canvas.mpl_connect('pick_event', picker)
fig, ax = plt.subplots()
for k in range(l3_count):
k = (k, )
ax.plot(data_bucket[k], gid=k, picker=5, ls='', marker='o')
fig.canvas.mpl_connect('pick_event', picker)
这是管理“当前”图中数据与下一层数据之间映射的棘手部分。所有mpl艺术家都有一个gid
属性,可以用来唯一地识别它们,所以在这里我使用+ +索引来生成一个字典的键,这是一个不同长度的整数元组。这只是在尝试制作合成3层数据时突然出现的第一件事。原则上任何使用所选艺术家的gid +该行中的索引的键控系统 - >下一层数据都可以使用。
然后,您可以对所有数字使用相同的选择器功能,只需关闭数据源即可。所有这些都可以(应该?)汇总成一个类。