备注
Go to the end 下载完整示例代码.
Pick 事件演示 2#
计算 100 个数据集的平均值 (mu) 和标准差 (sigma),并绘制 mu 与 sigma 的关系图.当您单击其中一个 (mu, sigma) 点时,绘制生成该点的原始数据集.
备注
此示例练习 Matplotlib 的交互功能,这不会出现在静态文档中.请在您的机器上运行此代码以查看交互性.
您可以复制和粘贴各个部分,或者使用页面底部的链接下载整个示例.

import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
X = np.random.rand(100, 1000)
xs = np.mean(X, axis=1)
ys = np.std(X, axis=1)
fig, ax = plt.subplots()
ax.set_title('click on point to plot time series')
line, = ax.plot(xs, ys, 'o', picker=True, pickradius=5)
def onpick(event):
if event.artist != line:
return
N = len(event.ind)
if not N:
return
figi, axs = plt.subplots(N, squeeze=False)
for ax, dataind in zip(axs.flat, event.ind):
ax.plot(X[dataind])
ax.text(.05, .9, f'mu={xs[dataind]:1.3f}\nsigma={ys[dataind]:1.3f}',
transform=ax.transAxes, va='top')
ax.set_ylim(-0.5, 1.5)
figi.show()
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()