【发布时间】:2020-06-02 15:28:01
【问题描述】:
我正在尝试在 3D 动画中绘制一条线。我无法擦除我在 update_line 中创建的图。问题是我总是添加新行。我看到的最佳解决方案是在函数之外创建线条并更新这些线条的数据,但我似乎无法找到 3D 绘图的方法
第一次发帖,只用了几个小时的python。对画布一无所知。请保持简单
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()
【问题讨论】:
标签: python python-3.x matplotlib animation plot