首页 > 解决方案 > Pandas 数据框的 3D 图

问题描述

我有一个 Pandas 数据框,它有 4 列 - 3 列数据,第 4 列表示簇号

        Col1       Col2       Col3  class
0      41.780730  -1.306569 -28.856727      0
1      36.421576  -7.654914 -32.675033      0
2      32.721677 -13.621913 -35.019806      0
3      42.028039 -12.280224 -39.521991      0
4      40.498716  -9.311412 -33.008553      0
...          ...        ...        ...    ...
14975 -54.303420  -5.444276  31.720262      1
14976 -63.230109 -12.673256  49.581873      1
14977 -63.609974 -11.004379  49.994068      1
14978 -52.682472 -10.969886  32.483284      1
14979 -37.329985 -19.290387  11.959876      1

[14980 rows x 4 columns]

我想创建一个 3D 散点图,前 3 列作为 X、Y、Z 轴值和表示它属于哪个类的类(用它来着色

我该怎么做?

标签: pythonpandasmatplotlib

解决方案


一个可能的解决方案是使用matplotlib's scatter3D()(请参阅此处的文档),例如

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

fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection='3d')
ax.scatter3D(df['Col1'], df['Col2'], df['Col3'], c=df['class'])
plt.show()

数据框变量df在哪里。


推荐阅读