首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从阴谋中移除点?

如何从阴谋中移除点?
EN

Stack Overflow用户
提问于 2018-07-24 13:15:29
回答 3查看 20K关注 0票数 1

我正在使用matplotlib.pyplot。

我想做以下几点:

  1. 我想要绘制一系列背景点(例如,单个蓝点)。
  2. 我添加了一个附加的系列点(例如3个黑点)
  3. 我保存了这个数字
  4. 我删除附加的系列点(黑色),并保留背景1(蓝色)。

我如何执行第4步?我想避免重新绘制背景点。

下面是一个缺少步骤4的代码示例。

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt

fig = plt.figure()

plt.xlim(-10,10)
plt.ylim(-10,10)

#step 1: background blue dot
plt.plot(0,0,marker='o',color='b')

#step 2: additional black dots
points_list = [(1,2),(3,4),(5,6)]
for point in points_list:
    plt.plot(point[0],point[1],marker='o',color='k')

#step 3: save
plt.savefig('test.eps')

#step 4: remove additional black dots
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-24 13:28:33

plot函数返回表示所绘制数据的Line2D对象列表。这些对象有一个remove方法,它将从绘制它们的图形中删除它们(请注意,Line2D继承自Artist,您可以通过Line2D.__mro__进行检查):

代码语言:javascript
运行
复制
remove() method of matplotlib.lines.Line2D instance
    Remove the artist from the figure if possible.  The effect
    will not be visible until the figure is redrawn, e.g., with
    :meth:`matplotlib.axes.Axes.draw_idle`.  Call
    :meth:`matplotlib.axes.Axes.relim` to update the axes limits
    if desired.

    [...]

因此,您可以执行以下操作(我一次就绘制了单个点):

代码语言:javascript
运行
复制
points = plt.plot(*zip(*points_list), 'o', color='k')[0]
# Remove the points (requires redrawing).
points.remove()

保持for循环如下:

代码语言:javascript
运行
复制
points = []
for point in points_list:
    points.extend(
        plt.plot(point[0], point[1], marker='o', color='k')
    )
for p in points:
    p.remove()

或者更简洁地使用列表理解:

代码语言:javascript
运行
复制
points = [plt.plot(*p, marker='o', color='k')[0] for p in points_list]
票数 2
EN

Stack Overflow用户

发布于 2018-07-24 13:25:27

您可以通过这样做来删除所绘制的点:

代码语言:javascript
运行
复制
temporaryPoints, = plt.plot(point[0],point[1],marker='o',color='k')
temporaryPoints.remove()
票数 2
EN

Stack Overflow用户

发布于 2018-07-24 13:19:24

您可以使用:

代码语言:javascript
运行
复制
#step 2
black_points, = plt.plot( zip(*points_list), marker="o", color="k")

#... step 3 ...
#...

#step 4
black_points.set_visible( False)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51499605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档