I am trying to plot a line in a 3D animation. I cant erase the plots i created in update_line. The problem is im always adding new lines. The best solution i see is to create the lines outside of the function and updating the data of those lines, but i cant seems to find a way for plot in 3D
first time posting, just a few hours of python. Learned nothing about canvas yet. Keep it simple please
import matplotlib; matplotlib.use("TkAgg")
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
dotinline = 101
nframe = 7
fig = plt.figure(figsize=(16, 8))
ax = p3.Axes3D(fig)
ax.view_init(0, 90)
def init():
ax.plot([-10, 10], [0, 0], [0, 0], "k")
ax.plot([0, 0], [-10, 10], [0, 0], "k")
ax.plot([0, 0], [0, 0], [-10, 10], "k")
ax.plot([], [], [], "b")
def update_lines(index):
seg = []
x = np.linspace(-10, 10, dotinline)
power = x**(-(nframe-1)/2+index)
for i in range(len(x)-1):
a = [x[i+1], x[i]]
b = [0, 0]
c = [power[i+1], power[i]]
seg.append([a, b, c])
for data in seg:
ax.plot3D(data[0], data[1], data[2], "-b")
ax.set_xlim3d([-10, 10])
ax.set_xlabel('X')
ax.set_ylim3d([-10, 10])
ax.set_ylabel('Y')
ax.set_zlim3d([-10, 10])
ax.set_zlabel('Z')
ax.set_title('3D Test')
line_ani = animation.FuncAnimation(fig, update_lines, init_func=init, frames=nframe, interval=2000, blit=False)
plt.show()
