Can't show animation for bar graph in Matplotl

2019-09-10 04:05发布

问题:

I have a text file that is constantly updating a single value. I'm trying to display this value using matplotlib's animation but I'm having lots of issues. For example I want to show a value that's changing every second on the text file (value ranges from 0 to 150) and I want the height of bar graph to increase and decrease with this value.

I want to do the same with horizontal graph. I also want to show a dot moving around the circle for different values (between 0 and 360).

I've tried to many things but it constantly broke the code or matplotlib froze. I've been trying to use matplotlib's animation.

Any help would be appreciated. When I run the code I don't see the bar graph

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import random

mf = r"data.txt"

fig = plt.figure()
ax = p3.Axes3D(fig)
xpos = 0
ypos = 0
zpos = 0
dx = 1
dy = 1





def update_bars():
    mmf = open(mf) 
    lines = mmf.readlines()
    depth_1 = lines[3]
    dz = depth_1


    bars = ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color= 'b')
    return bars

## add bars


ax.set_title('Depth')

line_ani = animation.FuncAnimation(fig, update_bars, 500, interval=100)
plt.show()

回答1:

Just had to change depth_1 = lines[3] to this depth_1 = int(lines[3])