【问题标题】:Matplotlib Button draw lineMatplotlib 按钮画线
【发布时间】:2018-06-21 13:04:42
【问题描述】:

我想创建一个可以显示(和用同一个按钮隐藏)一行的东西。

这是我目前拥有的:

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


fig, ax = plt.subplots()

class Index(object):
   ind = 0

   def test(self, event):
     self.plt.plot([0, 0], [1, 1])
     plt.draw()


callback = Index()
axtest = plt.axes([0.81, 0.05, 0.1, 0.075])
btest = Button(axtest, 'Test')
btest.on_clicked(callback.test)


plt.show()

有人可以帮我写这个脚本吗?我真的不知道该怎么做。

【问题讨论】:

  • 我运行您的代码并得到一个带有测试按钮的空图。我不确定你得到了什么或你想要达到什么。您能否发布您想要的图片或更好的描述?对不起,如果它很明显。但是看到这个stackoverflow.com/help/mcve

标签: python-2.7 matplotlib matplotlib-widget


【解决方案1】:

self.plt 没有意义。此外,您的 scipt 将始终添加新情节。相反,您可能想要切换现有绘图的可见性(并可能更改数据)。

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


fig, ax = plt.subplots()

class Index(object):
    def __init__(self, line):
        self.line = line

    def test(self, event):
        if self.line.get_visible():
            self.line.set_visible(False)
        else:
            # possibly change data here, for now same data is used
            self.line.set_data([0,1],[0,1])
            self.line.set_visible(True)
            self.line.axes.relim()
            self.line.axes.autoscale_view()
        self.line.figure.canvas.draw()

line, = plt.plot([],[], visible=False)

callback = Index(line)
axtest = plt.axes([0.81, 0.05, 0.1, 0.075])
btest = Button(axtest, 'Test')
btest.on_clicked(callback.test)


plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2018-07-12
    • 2021-02-25
    • 2015-07-04
    • 1970-01-01
    相关资源
    最近更新 更多