python - matplotlib 中带有颜色的 3d 散点图

标签 python matplotlib plot colors 3d

我在 python 中的 matplotlib 中使用散点 3d 图来绘制以下数据,

      x              y            z        b2
3.28912855713 4.64604863545 3.95526335139 1.0
3.31252670518 4.65385917898 3.96834118896 1.0

当绘图为二维时,

fig = plt.figure()
ax = fig.add_subplot(111)
line =ax.scatter(x,y,c=b2,s=500,marker='*',edgecolors='none')
cb = plt.colorbar(line)

它生成以下图像,这是完全正确的。 enter image description here

现在,我想绘制 3d 点,其代码是:

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
line = ax.scatter(x,y,z,c=b2,s=1500,marker='*',edgecolors='none',depthshade=0)
cb = plt.colorbar(line)

我得到了下面的图,这是不正确的,因为颜色应该像之前的情况一样是绿色的(b2 不会改变)。 enter image description here

我错过了什么吗?

最佳答案

可能不是。我不知道您使用的是哪个版本的 Matplotlib,但在某些时候,此问题存在一个错误,该错误显然仍然对我今天使用的版本(即 1.5.1)产生影响(请查看 this question 以了解更多信息) .

根据您的问题调整@Yann 解决方案应该可以解决该问题。我在自己的电脑上测试了一下,结果如下:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = [3.28912855713, 3.31252670518]
y = [4.64604863545, 4.65385917898]
z = [3.95526335139, 3.96834118896]
b2 = [1, 1]

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
line = ax.scatter(x, y ,z , c=b2, s=1500, marker='*', edgecolors='none', depthshade=0)
cb = plt.colorbar(line)

def forceUpdate(event):
    global line
    line.changed()

fig.canvas.mpl_connect('draw_event', forceUpdate)

plt.show()

入口处和旋转后的绘图图像为:

3D matplotlib scatterplot with color

3D matplotlib scatterplot with color after rotation

关于python - matplotlib 中带有颜色的 3d 散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39110336/

相关文章:

python - Psycopg2 或 SQLAlchemy 中的多个 SELECT

python - 在 Jupyter 笔记本中使用 PySpark 时如何包含外部 Spark 库

python - axes_divider 主机轴的 inset_axis 的纵横比(相对于大小和位置)

python - Matplotlib 在绘图时将最后一个点连接到第一个点

python - 如何将 3D 表面颜色图映射到标量函数?

python - 使用 Python 高效地绘制 csv 格式的表格

python - bash:mkvirtualenv:找不到命令

python - 使用非 bash shell 安装 Conda

python - 将时间序列数据集与缺失值对齐以进行绘图

python - 在 matplotlib 中使用ight_layout() 和不完整的轴数组 :