Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

matplotlib - Python plot not starting from center

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
enter image description here

Expected Output enter image description here

where I'm going wrong?

question from:https://stackoverflow.com/questions/65842190/python-plot-not-starting-from-center

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...