【问题标题】:How to use matplotlib animation within kivy如何在 kivy 中使用 matplotlib 动画
【发布时间】:2016-01-10 15:45:38
【问题描述】:

我想显示加速度传感器数据的实时图表。 Kivy 是框架,matplotlib 应该用于绘图。 我可以轻松地将静态图集成到我的应用程序屏幕中,但是我没有成功使用 matplotlib 动画方法。我怎样才能做到这一点?根据我发现的一个示例(不是为 kivy 制作的),以下是我目前的代码。

class FigGraphLive(BoxLayout):

def __init__(self, **kwargs):
    super(FigResultPropGraphLive, self).__init__(**kwargs)

    fig = Figure(figsize=(3, 1), facecolor='none', dpi=90)
    ax = fig.add_subplot(111)
    ax.set_axis_bgcolor('none')
    ax.spines['bottom'].set_color('white')
    ax.spines['top'].set_color('white')
    ax.spines['left'].set_color('white')
    ax.spines['right'].set_color('white')
    xa = ax.xaxis                  
    ya = ax.yaxis
    xa.set_tick_params(labelcolor='white')
    xa.set_tick_params(color='white')
    ya.set_tick_params(labelcolor='white')
    ya.set_tick_params(color='white')

    self.valx = np.zeros(0)
    self.valy = np.zeros(0)
    self.valz = np.zeros(0)
    self.time = np.zeros(0)
    plxax = ax.plot(self.time, self.valx, color='blue', label='X-Axis')
    plyax = ax.plot(self.time, self.valy, color='red', label='Y-Axis')
    plzax = ax.plot(self.time, self.valz, color='green', label='Z-Axis')

    leg = ax.legend()
    leg.get_frame().set_alpha(0.4)

    self.plxmin = 0.0
    self.plxmax = 10.0
    self.plx = 0.0

    simulation = mplanimation.FuncAnimation(fig, self.updateGraph, blit=False, frames=200, interval=50, repeat=False)

    figcanvas = FigureCanvas(fig)
    figcanvas.draw()

    self.create_plot(figcanvas)

def updateGraph(self):
    self.valx = np.append(self.valx, np.random.rand() * 4)
    self.valy = np.append(self.valx, np.random.rand() * 4)
    self.valz = np.append(self.valx, np.random.rand() * 0.5)

    self.plx += 0.05

    if self.plx >= self.plxmax - 1.00:
        plxax.axes.set_xlim(self.plx - self.plxmax + 1.0, x + 1.0)
        plyax.axes.set_xlim(self.plx - self.plxmax + 1.0, x + 1.0)
        plzax.axes.set_xlim(self.plx - self.plxmax + 1.0, x + 1.0)

    return plxax, plyax, plzax

def create_plot(self, figcanvas):
    self.add_widget(figcanvas)

【问题讨论】:

    标签: python animation matplotlib kivy


    【解决方案1】:

    您可以使用 kivy garden 中的 matplotlib 集成示例:https://github.com/kivy-garden/garden.matplotlib


    另一个选项是 https://github.com/kivy-garden/garden.graph ,将动画演示嵌入到 init.py 文件中。

    【讨论】:

    • 也许我只是不明白,但那里给出的示例似乎没有包括使用 matplotlib 动画的方法,因为我看到了它。我没有问题,包括图表本身,但动画它:-)
    • @Jonnnny 也许这个会更好:github.com/kivy-garden/garden.graph,只需运行 init.py,演示文稿中有一个动画正弦。
    猜你喜欢
    • 2017-12-07
    • 1970-01-01
    • 2021-12-16
    • 2018-10-06
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多