【问题标题】:Make dots in matplotlib plots selectable by mouse使 matplotlib 图中的点可由鼠标选择
【发布时间】:2013-09-13 15:05:45
【问题描述】:

scikit-learn 有一个非常好的演示,它创建了一个异常值分析工具。这里是

import numpy as np
import pylab as pl
import matplotlib.font_manager
from scipy import stats

from sklearn import svm
from sklearn.covariance import EllipticEnvelope

# Example settings
n_samples = 200
outliers_fraction = 0.25
clusters_separation = [0, 1, 2]

# define two outlier detection tools to be compared
classifiers = {
    "One-Class SVM": svm.OneClassSVM(nu=0.95 * outliers_fraction + 0.05,
                                     kernel="rbf", gamma=0.1),
    "robust covariance estimator": EllipticEnvelope(contamination=.1)}

# Compare given classifiers under given settings
xx, yy = np.meshgrid(np.linspace(-7, 7, 500), np.linspace(-7, 7, 500))
n_inliers = int((1. - outliers_fraction) * n_samples)
n_outliers = int(outliers_fraction * n_samples)
ground_truth = np.ones(n_samples, dtype=int)
ground_truth[-n_outliers:] = 0

# Fit the problem with varying cluster separation
for i, offset in enumerate(clusters_separation):
    np.random.seed(42)
    # Data generation
    X1 = 0.3 * np.random.randn(0.5 * n_inliers, 2) - offset
    X2 = 0.3 * np.random.randn(0.5 * n_inliers, 2) + offset
    X = np.r_[X1, X2]
    # Add outliers
    X = np.r_[X, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))]

    # Fit the model with the One-Class SVM
    pl.figure(figsize=(10, 5))
    for i, (clf_name, clf) in enumerate(classifiers.items()):
        # fit the data and tag outliers
        clf.fit(X)
        y_pred = clf.decision_function(X).ravel()
        threshold = stats.scoreatpercentile(y_pred,
                                            100 * outliers_fraction)
        y_pred = y_pred > threshold
        n_errors = (y_pred != ground_truth).sum()
        # plot the levels lines and the points
        Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
        Z = Z.reshape(xx.shape)
        subplot = pl.subplot(1, 2, i + 1)
        subplot.set_title("Outlier detection")
        subplot.contourf(xx, yy, Z, levels=np.linspace(Z.min(), threshold, 7),
                         cmap=pl.cm.Blues_r)
        a = subplot.contour(xx, yy, Z, levels=[threshold],
                            linewidths=2, colors='red')
        subplot.contourf(xx, yy, Z, levels=[threshold, Z.max()],
                         colors='orange')
        b = subplot.scatter(X[:-n_outliers, 0], X[:-n_outliers, 1], c='white')
        c = subplot.scatter(X[-n_outliers:, 0], X[-n_outliers:, 1], c='black')
        subplot.axis('tight')
        subplot.legend(
            [a.collections[0], b, c],
            ['learned decision function', 'true inliers', 'true outliers'],
            prop=matplotlib.font_manager.FontProperties(size=11))
        subplot.set_xlabel("%d. %s (errors: %d)" % (i + 1, clf_name, n_errors))
        subplot.set_xlim((-7, 7))
        subplot.set_ylim((-7, 7))
    pl.subplots_adjust(0.04, 0.1, 0.96, 0.94, 0.1, 0.26)

pl.show()

这是它的样子:

是不是很酷?

但是,我希望情节是鼠标敏感的。也就是说,我希望能够通过工具提示或弹出窗口或滚动条中的内容单击点并找出它们是什么。而且我还希望能够点击缩放,而不是使用边界框进行缩放。

有什么办法吗?

【问题讨论】:

标签: python matplotlib tkinter


【解决方案1】:

不要过多地插入我自己的项目,而是看看mpldatacursor。如果您愿意,从头开始实施也很容易。

举个简单的例子:

import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor

x1, y1 = np.random.random((2, 5))
x2, y2 = np.random.random((2, 5))

fig, ax = plt.subplots()
ax.plot(x1, y1, 'ro', markersize=12, label='Series A')
ax.plot(x2, y2, 'bo', markersize=12, label='Series B')
ax.legend()

datacursor()
plt.show()

要使此代码与您发布的示例代码一起使用,您需要稍作更改。实际上,艺术家标签是在对图例的调用中设置的,而不是在创建艺术家时设置的。这意味着无法检索特定艺术家的图例中显示的内容。您需要做的只是将标签作为 kwarg 传递给 scatter,而不是作为第二个参数传递给 legend,然后事情就会如您所愿。

【讨论】:

  • 我很高兴你能插入你自己的项目。谢谢谢谢!我需要查看您的主页。
  • 我将 datacursor() 添加到我的示例程序 它会导致 python 崩溃。有兴趣查看故障转储吗?
  • 嗯,这出乎意料!您可以在某处发布回溯吗?谢谢!
  • 我做到了!见github.com/joferkington/mpldatacursor/issues。我已经用 MacPython 2.7 和 3.2 和 3.3 验证了这台两台不同的计算机。目前尚不清楚问题出在哪里。我在 Linux 上没有看到崩溃,也没有测试过 Windows。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
  • 2013-07-04
相关资源
最近更新 更多