【问题标题】:Matplotlib: How to plot points on my graph instead of painting them on a button?Matplotlib:如何在我的图表上绘制点而不是在按钮上绘制它们?
【发布时间】:2021-02-26 04:51:45
【问题描述】:

编辑:ax.scatter(event.xdata, event.ydata) 工作正常,IDK 我是怎么过去的。但是,按下按钮仍然是画点,我想知道过滤坐标是否是解决这个问题的好习惯。

这是我在轴上绘制一些点的最小代码。不幸的是,按钮代码plt.scatter() 停止正常工作(点开始出现在按钮顶部)。对我有什么建议吗?

import matplotlib.pyplot as plt
from matplotlib.widgets import Button

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])


def onclick(event):
    plt.scatter(event.xdata, event.ydata)
    fig.canvas.draw()


cid = fig.canvas.mpl_connect('button_press_event', onclick)


def do(event):
    print(1)


bprev = Button(plt.axes([0.7, 0.05, 0.1, 0.075]), 'Previous')
bprev.on_clicked(do)

plt.show()

【问题讨论】:

  • 赞成伟大的(虽然是无意的)效果。不过,让它ax.scatter(event.xdata, event.ydata)。
  • @Mr.T 谢谢!!! IDK我是怎么过去的......然后我想我会通过坐标过滤点击以避免在点击按钮时绘画。

标签: python matplotlib matplotlib-widget


【解决方案1】:

要解决第一个问题,正如 T. 先生指出的那样,您可以使用 ax.scatter(event.xdata, event.ydata) 在主轴上绘图,而不是在按钮上。

要解决单击按钮时点被绘制的问题,您可以使用event.inaxes 属性,它告诉您鼠标单击的Axes 实例。所以,如果event.inaxes和ax一样,画点;如果不是,那么它在按钮中,所以什么都不做。

在你的函数中,它看起来像这样:

def onclick(event):
    if event.inaxes == ax:
        ax.scatter(event.xdata, event.ydata)
        fig.canvas.draw()

【讨论】:

    猜你喜欢
    • 2014-04-29
    • 1970-01-01
    • 2014-11-02
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2020-02-06
    相关资源
    最近更新 更多