如何在 Matplotlib 中绘制带有颜色条的 3D 图形中的散点?
matplotlibpythondata visualization更新于 2025/6/6 17:16:17
要在 matplotlib 中绘制带有颜色条的 3D 图形中的散点,我们可以使用 scatter() 和 colorbar() 方法。
步骤
设置图形大小并调整子图之间和周围的填充。
使用 figure() 方法创建新图形或激活现有图形。
添加轴作为子图排列。
使用 numpy 创建 xs、ys 和 zs 数据点。
使用 scatter() 方法创建散点图。
使用colorbar() 方法,带有 scatter scalar 可映射 colorbar 实例。
要显示图形,请使用 show() 方法。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(projection="3d") xs = np.random.rand(100) ys = np.random.rand(100) zs = np.random.rand(100) sc = ax.scatter(xs, ys, zs, c=xs, cmap="copper") plt.colorbar(sc) plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

