python - 如何使 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 秒才能完全加载图像。旋转图像时,代码仍然相当慢。
解决方案
推荐阅读
- swift - Swift IOS - 一个复杂的表格视图单元格的排列
- python - 更改 Python:在 Visual Studio Code 的终端命令中运行 Python 文件
- python - MongoDB 更新多个嵌套数组
- node.js - 我可以部署一个前端文件夹和后端文件夹都在同一个 Github 存储库上的 MERN 应用程序吗?
- sql - 查看不执行,但直接运行相同的查询
- typo3 - TYPO3 9 Extbase EXT originalResource 的 FileReference 始终为 NULL。为什么?
- css - 如何使用变换矩阵修改 svg 路径数据
- sql - 如何根据查询中的行多次替换?
- javascript - 反应钩子,状态没有正确改变
- regex - elasticsearch 正则表达式模式