我運行以下代碼為移動球體設定影片,其中坐標位于文本檔案中:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd
df = pd.read_csv('/path/to/text/file', sep=" ", header=None)
fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
r = 4
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)
x0 = r * np.outer(np.cos(u), np.sin(v)) df[1][0]
y0 = r * np.outer(np.sin(u), np.sin(v)) df[2][0]
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) df[3][0]
surface_color = "tab:blue"
def init():
ax.plot_trisurf(x0, y0, z0, linewidth=0, antialiased=False)
return fig,
def animate(i):
# remove previous collections
ax.collections.clear()
x = df[1][i]
y = df[2][i]
z = df[3][i]
# add the new sphere
ax.plot_trisurf(x, y, z, linewidth=0, antialiased=False)
return fig,
ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 500, interval = 2)
plt.show()
我收到以下錯誤“ValueError:x 和 y 必須是等長的一維陣列”,即使我確定陣列大小相同。如何使它們大小相等并解決此錯誤?
作為檔案中內容的示例:
0.196812 19.992262 29.989437 30.040883 0.080273 39.999358 30.009271 30.052325
0.288626 19.998165 29.986778 30.083568 0.305931 39.993330 30.011351 30.126911
0.080401 20.012453 29.982994 30.138681 0.224338 39.986476 30.010048 30.204666
0.380893 20.017042 29.984149 30.196864 0.289713 39.984835 30.009015 30.285159
0.396571 20.009539 29.998625 30.259610 0.350441 39.993791 30.017738 30.361558
0.647959 20.012771 29.995641 30.328414 0.275493 39.992826 30.019380 30.433242
0.741711 20.000002 29.978545 30.397738 0.248958 39.992041 30.010427 30.508367
0.867323 19.991656 29.971294 30.464908 0.313612 39.999097 30.004667 30.591674
文本檔案非常大,大約 20,000 行。
uj5u.com熱心網友回復:
如果您要繪制的曲面具有引數方程(例如球體),請使用網格網格方法(x, y, z必須是二維陣列)并呼叫ax.plot_surface. 相反,您使用了 1D 陣列,后來又呼叫ax.plot_trisurf了 : 當使用網格網格方法不容易表示表面時(這不是您的情況),此函式更適合。不要讓你的生活復雜化:保持簡單!
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
from matplotlib import animation
import pandas as pd
df = pd.read_csv('path/to/file', sep=" ", header=None)
fig = plt.figure(facecolor='black')
ax = plt.axes(projection = "3d")
u = np.linspace(0, 2*np.pi, 40)
v = np.linspace(0, np.pi, 20)
u, v = np.meshgrid(u, v)
r = 4
ax.set_xlim(0, 60)
ax.set_ylim(0, 60)
ax.set_zlim(0, 60)
x0 = r * np.outer(np.cos(u), np.sin(v)) df[1][0]
y0 = r * np.outer(np.sin(u), np.sin(v)) df[2][0]
z0 = r * np.outer(np.ones(np.size(u)), np.cos(v)) df[3][0]
surface_color = "tab:blue"
def init():
ax.plot_surface(x0, y0, z0, color=surface_color)
return fig,
def animate(i):
# remove previous collections
ax.collections.clear()
x = df[1][i]
y = df[2][i]
z = df[3][i]
# add the new sphere
ax.plot_surface(x0 x, y0 y, z0 z, color=surface_color)
return fig,
ani = animation. FuncAnimation(fig, animate, init_func = init, frames = 500, interval = 2)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/472003.html
標籤:Python 数组 熊猫 matplotlib 动画片
上一篇:在reactnative的功能組件中使用animateCamera
下一篇:移動球體影片
