I want to draw a plot inside tkinter using Animation function of matplotlib. Now I save the x and y in a file and read from the file to plot the graph. Since I want my graph to remain in center I set the xlim in that..but it's not working..rather my graph is starting from o point and not from the center.
def animate(i):
global j
xs = []
ys = []
pid = os.getpid()
p = psutil.Process(pid=pid)
p.cpu_percent(interval=None)
usage = p.cpu_percent(interval=None)
with open("harry.txt", "a") as f:
f.write(str(usage))
f.write(",")
f.write(str(j))
f.write("
")
graph_data = open('harry.txt', 'r').read()
lines = graph_data.split('
')
for line in lines:
if len(line) > 1:
y, x = line.split(',')
xs.append(x)
ys.append(y)
ax.plot(xs,ys,color='b')
ax.set_xlim(left=max(0, j - 50), right=j + 50)
j = j + 1
return line,
root = Tk.Tk()
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(column=0,row=1)
ax = fig.add_subplot(111)
ax.clear()
line, = ax.plot(xs,ys,color='b')
ani = animation.FuncAnimation(fig, animate, interval=1000, blit=False)
Tk.mainloop()
Output that I'm getting
Expected Output
where I'm going wrong?
question from:
https://stackoverflow.com/questions/65842190/python-plot-not-starting-from-center 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…