【问题标题】:How to display my plot in the graph?如何在图表中显示我的情节?
【发布时间】:2017-03-03 10:08:39
【问题描述】:

您好,我的 python 代码有问题。我想创建一个图,为此我有一个按钮,我想要的是当我单击按钮时,我会在我想要的图表上得到图。现在我得到的是一个情节,但在按钮上!

这是我的代码:

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

class Index(objects):
    ind = 0
    def next(self,event):
        a = np.array([1,2,3,4,5])
        b = np.array([15,20,1,3,5])
        v = plt.plot(a,b)


callback = Index()
axnext = plt.axes([0.0, 0.90, 0.1, 0.075])
bnext = Button(axnext, 'Plot')
bnext.on_clicked(callback.next)

plt.show()

感谢您的帮助!

【问题讨论】:

  • 应该将class Index(objects) 改为class Index(object) 吗?
  • 进行该更改确实会绘制它,但它会绘制在按钮内?我会继续努力,看看我能不能解决它

标签: python python-2.7 python-3.x numpy matplotlib


【解决方案1】:

好的,我想我已经解决了:

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

figure, ax1 = plt.subplots()

class Index(object):

    ind = 0

    def next(self, event):    
        a = np.array([1,2,3,4,5])
        b = np.array([15,20,1,3,5])
        ax1.plot(a,b)


callback = Index()
axnext = plt.axes([0.0, 0.90, 0.1, 0.075])
bnext = Button(axnext, 'Plot')
bnext.on_clicked(callback.next)

plt.show()

所以我在这里将class Index(objects) 更改为class Index(object)。我还在课前定义了一个图形和一些简单的轴,这意味着您可以绘制到该图形。如果没有它,它似乎会在“绘图”按钮内绘制您的图表。

【讨论】:

    猜你喜欢
    • 2021-04-11
    • 1970-01-01
    • 2016-01-08
    • 2016-07-07
    • 2017-11-07
    • 1970-01-01
    • 2010-11-23
    • 2020-07-02
    • 2020-05-05
    相关资源
    最近更新 更多