如何从matplotlib图中删除点? [英] How do you remove a point from matplotlib plot?

查看:52
本文介绍了如何从matplotlib图中删除点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在matplotlib中有一个图,类似这样:

Lets say you have a plot in matplotlib, something like that:

figure = Figure()
figureCanvas = FigureCanvas(figure)
axes = figure.add_subplot(111)
axes.plot([1, 2, 3], [2, 3, 1], linestyle = "None", marker = "o", color = '#1f77b4', markersize = 3)

这将给您3分的情节.如何从绘图中删除特定点,而不重新绘制整个内容?

This would give you a plot with 3 points. How do I remove a specific point from plot, without redrawing the whole thing again?

推荐答案

首先,你至少需要重新绘制绘图(Line2D 对象),否则在阴谋.

First of all, you need to redraw at least the plot (the Line2D object), otherwise there will be no change in the plot.

不知道不重画的目的,很难判断一个可接受的解决方案.但是,通常您只需要重画整个画布即可.要设置新数据,可以使用 Line2D.set_data()方法,如下所示.您可以按该点的数字键(0,1,2)在图中删除.

Without knowing the purpose of not redrawing, it's hard to judge on an acceptable solution. However, usually you would just redraw the whole canvas. To set new data, the Line2D.set_data() method can be used as shown in the following. You may press the number key (0,1,2) of the point to remove in the plot.

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [2, 3, 1]

fig, ax = plt.subplots()
line, = ax.plot(x, y, ls="None", marker="o", color='#1f77b4', ms=10)

def remove_point(event):
    try:
        key = int(event.key)
        xvals = x[:]
        xvals.pop(key)
        yvals = y[:]
        yvals.pop(key)
        line.set_data(xvals,yvals)
        fig.canvas.draw_idle()
    except:
        pass

fig.canvas.mpl_connect('key_press_event', remove_point)
ax.set_title("Press number of point to remove")
plt.show()

这篇关于如何从matplotlib图中删除点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆