【问题标题】:How to increase speed of matplotlib scatter plot?如何提高 matplotlib 散点图的速度?
【发布时间】:2019-01-22 05:18:27
【问题描述】:

这是我的代码:

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 可能不是绘制散点图的最有效工具。如果是真的,换库是不是很困难?这只是我代码的一部分,而且我正在使用画布。

【问题讨论】:

    标签: python matplotlib 3d scatter


    【解决方案1】:

    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()
    

    【讨论】:

    • 谢谢,但我没有运行它。我在安装 mayavi 时遇到问题。我尝试通过 pip install mayavi 安装它,但总是出现此错误: Command "python setup.py egg_info" failed with error code 1 in C:\Users\xxx\AppData\Local\Temp\pip-install-yzxn3cgx\mayavi \
    • 您在 Windows 上吗?然后尝试来自gohlke 的软件包。还要安装 VTK,如链接中所述。
    • @Jannick 现在可以正常工作了。它非常光滑,我喜欢它的外观。但现在出现了一些新问题。我需要我的颜色条(基于我的数组的第二个条目(15 ...40),就像我在我的 matplotlib 解决方案中一样。我插入了一个图例,但范围很奇怪(从 0 -20),实际上我没有知道此数据的来源。此外,我将此图嵌入到使用 pyqt5 和使用画布制作的 GUI 中。有没有办法对这个 mayavi 图做同样的事情?最后一个问题是坐标系。有可能在matplotlib中插入一个?也许我应该回到matplotlib???
    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多