如何提高matplotlib散点图的速度?

问题描述 投票:0回答:1

这是我的代码:

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。

python matplotlib 3d scatter
1个回答
2
投票

Matplotlib不是为3d图设计的,目标是高质量(可打印)图形而不是速度。我会使用另一个像mayavi这样的库进行三维可视化。这是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()
© www.soinside.com 2019 - 2024. All rights reserved.