python - 在 3D 绘图 + 热图中绘制 4 个变量
问题描述
我在数据框中有 4 列,我想看看是否存在相关性。我认为通过将它们绘制在 3D 图中然后将第 4 维添加为热图可以给我一些见解,但我不知道如何将此热图添加到数据框中的一列。
这是我到目前为止所得到的:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(12,12));
ax = fig.add_subplot(111, projection='3d');
ax.scatter(Series1,Series2,Series3);
这给了我这个:
( https://i.stack.imgur.com/qRGMv.png )
所以,有Series1
,Series2
和上面的点Series3
......但是有没有办法添加热图或任何东西来区分Series4
?
解决方案
我发现有一种方法可以通过添加 c 参数来添加颜色图,如下所示:
withcolormap = ax.scatter(Series1, Series2, Series3, c = Series4, cmap='gist_heat');
然后将其 colobar 放在一边:
fig.colorbar(withcolormap, shrink=0.75);
由于它给了我任何见解,我想知道如何绘制该图的旋转视图的动画,我通过执行以下代码进行了尝试:
from matplotlib.animation import FuncAnimation, PillowWriter
def rotation(i):
ax = fig.add_subplot(111, projection='3d')
ax.view_init(elev=30, azim=i);
ani = FuncAnimation(withcolormap, rotation, frames=range(0,360,10))
然后它返回一个错误:
AttributeError: 'Path3DCollection' object has no attribute 'canvas'