【发布时间】:2017-06-17 01:24:53
【问题描述】:
我有一个包含四列测量数据的 pandas 数据框。我想创建一个 3D 曲面图,其中行索引为 X,列索引为 Y,数据为 Z。(每列中的数据是一系列离散测量输出的测试,该测试通过所有值逐步执行每个类别 Y 的 X)
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 4), columns=['A', 'B', 'C', 'D'])
print(df)
A B C D
0 0.791692 -0.945571 0.183304 2.039369
1 -0.474666 1.117902 -0.483240 0.137620
2 1.448765 0.228217 0.294523 0.728543
3 -0.196164 0.898117 -1.770550 1.259608
4 0.646730 -0.366295 -0.893671 -0.745815
我尝试使用 np.meshgrid 将 df 转换为 numpy 网格,如下所示,但不确定我是否真的理解所需的内容,或者我是否可以以这种方式使用 df 索引。
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = df.columns
y = df.index
X,Y = np.meshgrid(x,y)
Z = df
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
我在这里通读了 matplotlib 3D 教程和相关答案,但仍然卡住了。如果有人能指出我正确的方向,将不胜感激。
【问题讨论】:
标签: python pandas numpy matplotlib 3d