首页 > 解决方案 > 如何使 matplotlib 加载更快?

问题描述

这段代码需要花费大量时间来运行约 33 分钟,我怎样才能让它更快。

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

points = [[x, y, z] for x in range(256) for y in range(256) for z in range(256)]
# --- ~11.32s ---
# rgb length = 16777216

ax.scatter([x[0] for x in points], [y[1] for y in points], [z[2] for z in points],
           color=[[color[0] / 255, color[1] / 255, color[2] / 255] for color in points])
# --- ~11m ---

plt.show()
# --- ~12m ---

在此处输入图像描述

编辑:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

every_point = [[x, y, z] for x in range(256) for y in range(256) for z in range(256)]
# rgb length = 16777216

points = [loc for loc in every_point if 255 in loc or 0 in loc]
# rgb length = 390152
# removed all unnecessary "below skin" points. only points visible will be loaded

ax.scatter([x[0] for x in points], [y[1] for y in points], [z[2] for z in points],
           color=[[color[0] / 255, color[1] / 255, color[2] / 255] for color in points])

plt.show()
# --- 46s ---

这段代码使它快了近 50 倍,大约 46 秒才能完全加载图像。旋转图像时,代码仍然相当慢。

标签: pythonmatplotlib3d

解决方案


推荐阅读