Issue
Well this How to use matplotlib animation within kivy question is similar but the answer is just not at all valuable Also as pointed by @ImportanceOfBeingErnest I edited the question to Can we actually do it?If Yes then proceed below.
So it was easy to add animation in tkinter according to the following tutorial Sentdex tutorial on how to add matplotlib animation in tkinter
I tried to do the same in kivy but cannot figure out where to write the line ani=animation.Funcanimation(blabla)
See the last line in the following code
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)
Solution
I think kivy is less used nowadays.Nobody answered So i somehow i figured out a alternative to what i wanted to achieve. I added a Refresh button which runs the whole fuction again which includes fetching of the values from a file and draws the graph again.Thus whenever the values are updated in the file we get a plot for that.
Here's the python 3 code.
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()
and here's the corresponding kivy code.
<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
So finally after running my complete code kivy application looks like this- Kivy application image
Answered By - 5cube
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.