这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import cm
#from matplotlib.patches import Circle
ri = 100
ra = 300
h=20
# input xy coordinates
xy = np.array([[ri,0],[ra,0],[ra,h],[ri,h],[ri,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 30 steps
phi = np.linspace(0, 2*np.pi, 50)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 30 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.set_zlim(0,200)
ax.plot_surface(X, Y, Z, alpha=0.5, color='grey', rstride=1, cstride=1, linewidth=0, edgecolor='none')
arr = np.array([[100, 15],
[114.28, 17],
[128.57, 18],
[142.85, 24],
[157.13, 26],
[171.13, 28],
[185.69, 29],
[199.97, 30],
[214.25, 31],
[228.53, 32],
[242.81, 35],
[257.09, 36],
[271.37, 37],
[288.65, 40]])
#interpolating between the single values of the arrays
new_x = np.concatenate([np.linspace(arr[i,0],arr[i+1,0], num=20)
for i in range(len(arr)-1)])
new_y = np.interp(new_x, arr[:,0], arr[:,1])
t=np.arange(260)
tmp_phi = np.linspace(0,2*np.pi,20)[:,None] # angle data
linesurf_x = new_x*np.cos(tmp_phi)
linesurf_y = new_x*np.sin(tmp_phi)
linesurf_z = np.broadcast_to(new_y, linesurf_x.shape)
linesurf_c = np.broadcast_to(t, linesurf_x.shape) # color according to t
colors = cm.jet(linesurf_c/linesurf_c.max()) # grab actual colors for the surface
ax.plot_surface(linesurf_x, linesurf_y, 1.5*linesurf_z, facecolors=colors,
rstride=1, cstride=3, linewidth=1, edgecolor='none')
cax, _ = mpl.colorbar.make_axes(plt.gca(), shrink=0.8)
cax.yaxis.set_ticks_position('right')
cbar = mpl.colorbar.ColorbarBase(cax, cmap='jet', label='test',
norm=mpl.colors.Normalize(vmin=15, vmax=41))
plt.show()
问题在于速度。计算时间很长,但这不是最大的问题。绘制图形后,当我尝试旋转图形时,它会变得很滞后...是否有可能无需付出太多努力就能提高速度?我用谷歌搜索,我读到也许matplotlib并不是绘制散点图的最有效工具。如果属实,更改库是否很困难?这只是我代码的一部分,而且我正在使用canvas。
答案 0 :(得分:2)
Matplotlib不是为3D图设计的,而是针对高质量(可打印)图形的,而不是针对速度的。我将使用另一个库(例如mayavi)进行3d可视化。这是通过mayavi可视化实现的代码
import numpy as np
import mayavi.mlab as mlab
ri = 100
ra = 300
h=20
# input xy coordinates
xy = np.array([[ri,0],[ra,0],[ra,h],[ri,h],[ri,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 30 steps
phi = np.linspace(0, 2*np.pi, 50)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 30 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)
mlab.mesh(X, Y, Z, color=(0.4,0.4,0.4))
arr = np.array([[100, 15],
[114.28, 17],
[128.57, 18],
[142.85, 24],
[157.13, 26],
[171.13, 28],
[185.69, 29],
[199.97, 30],
[214.25, 31],
[228.53, 32],
[242.81, 35],
[257.09, 36],
[271.37, 37],
[288.65, 40]])
#interpolating between the single values of the arrays
new_x = np.concatenate([np.linspace(arr[i,0],arr[i+1,0], num=20)
for i in range(len(arr)-1)])
new_y = np.interp(new_x, arr[:,0], arr[:,1])
t=np.arange(260)
tmp_phi = np.linspace(0,2*np.pi,20)[:,None] # angle data
linesurf_x = new_x*np.cos(tmp_phi)
linesurf_y = new_x*np.sin(tmp_phi)
linesurf_z = np.broadcast_to(new_y, linesurf_x.shape)
linesurf_c = np.broadcast_to(t, linesurf_x.shape) # color according to t
mlab.mesh(linesurf_x, linesurf_y, 1.5*linesurf_z,scalars=linesurf_c)
mlab.show()