gpt4 book ai didi

python - 我们可以在kivy中使用matplotlib.animation吗?如果是,如何?

转载 作者:行者123 更新时间:2023-12-01 08:33:28 25 4
gpt4 key购买 nike

嗯,这个How to use matplotlib animation within kivy问题很相似,但答案根本没有值(value)另外,正如 @ImportanceOfBeingErnest 所指出的,我将问题编辑为“我们真的可以这样做吗?如果是,则继续下面的操作。

所以根据以下教程在 tkinter 中添加动画很容易 Sentdex tutorial on how to add matplotlib animation in tkinter

我尝试在 kivy 中执行相同的操作,但无法弄清楚在哪里编写该行 ani=animation.Funcanimation(blabla) 请参阅以下代码中的最后一行

class Graph(FigureCanvasKivyAgg):
def __init__(self,*args,**kwargs):
FigureCanvasKivyAgg.__init__(self,f)
pullData = open("TimeLatitude.txt", "r").read()
dataList = pullData.split('\n')
xList = []
yList = []
pullData2 = open("TimeLongitude.txt", "r").read()
dataList2 = pullData2.split('\n')
xList2 = []
yList2 = []
for eachLine in dataList:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xList.append(int(x))
yList.append(int(y))
for eachLine in dataList2:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xList2.append(int(x))
yList2.append(int(y))
a.clear()
a.plot(xList, yList, "#00A3E0", label="Latitude")
a.plot(xList2, yList2, "#183A54", label="Longitude")
a.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3,
ncol=2, borderaxespad=0)
title = "Oxymora Mars Rover Geographic Points\nLast Longitude : " + str(
yList2[len(yList2) - 1]) + "\nLast Latitude : " + str(yList[len(yList) - 1])
a.set_title(title)
ani = animation.FuncAnimation(f, animate, interval=1000)

最佳答案

我认为现在 kivy 的使用较少。没有人回答所以我以某种方式找到了我想要实现的目标的替代方案。我添加了一个刷新按钮,它再次运行整个功能,其中包括从文件中获取值并再次绘制图表。因此,每当文件中的值更新时,我们都会得到一个绘图。

这是 python 3 代码。

class Graph(FigureCanvasKivyAgg):
def __init__(self,*args,**kwargs):
FigureCanvasKivyAgg.__init__(self,f)
pullData = open("TimeLatitude.txt", "r").read()
dataList = pullData.split('\n')
xList = []
yList = []
pullData2 = open("TimeLongitude.txt", "r").read()
dataList2 = pullData2.split('\n')
xList2 = []
yList2 = []
for eachLine in dataList:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xList.append(int(x))
yList.append(int(y))
for eachLine in dataList2:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xList2.append(int(x))
yList2.append(int(y))
a.plot(xList, yList, "#00A3E0", label="Latitude")
a.plot(xList2, yList2, "#183A54", label="Longitude")
a.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3,
ncol=2, borderaxespad=0)
title = "Oxymora Mars Rover Coordinates\nLast Longitude : " + str(
yList2[len(yList2) - 1]) + "\nLast Latitude : " + str(yList[len(yList) - 1])
a.set_title(title)
def animate(self):
pullData = open("TimeLatitude.txt", "r").read()
dataList = pullData.split('\n')
xList = []
yList = []
pullData2 = open("TimeLongitude.txt", "r").read()
dataList2 = pullData2.split('\n')
xList2 = []
yList2 = []
for eachLine in dataList:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xList.append(int(x))
yList.append(int(y))
for eachLine in dataList2:
if len(eachLine) > 1:
x, y = eachLine.split(',')
xList2.append(int(x))
yList2.append(int(y))
a.clear()
a.plot(xList, yList, "#00A3E0", label="Latitude")
a.plot(xList2, yList2, "#183A54", label="Longitude")
a.legend(bbox_to_anchor=(0, 1.02, 1, .102), loc=3,
ncol=2, borderaxespad=0)
title = "Oxymora Mars Rover Coordinates\nLast Longitude : " + str(
yList2[len(yList2) - 1]) + "\nLast Latitude : " + str(yList[len(yList) - 1])
a.set_title(title)
self.draw()

这是相应的 kivy 代码。

<MainScreen>:
name: "main"
FloatLayout:
Graph
id:gr
Button:
on_release: gr.animate()
text: "Refresh"
font_size: 15
size_hint:0.068,0.05
pos_hint: {"x":0,"top":0.8}
color: 1,0,1,1

最后,在运行我的完整代码后,kivy 应用程序看起来像这样 - Kivy application image

关于python - 我们可以在kivy中使用matplotlib.animation吗?如果是,如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53830982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com