Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...