Python 将颜色贴图应用于Matplotlib 3D曲面中的自定义轴

Python 将颜色贴图应用于Matplotlib 3D曲面中的自定义轴,python,matplotlib,surface,mplot3d,colormap,Python,Matplotlib,Surface,Mplot3d,Colormap,我有timeseries数据,我已经将其分割成数百个块。我解决了每个线段的自相关,并绘制了它们: # plot superimposed fig = plt.figure() color = iter(plt.cm.Set2(np.linspace(0,1,num_segs))) seg_iterator = df.iterrows() for index, seg in seg_iterator: # iterate over dataframe c=next(color)

我有timeseries数据,我已经将其分割成数百个块。我解决了每个线段的自相关,并绘制了它们:

# plot superimposed
fig = plt.figure()
color = iter(plt.cm.Set2(np.linspace(0,1,num_segs)))
seg_iterator = df.iterrows() 
for index, seg in seg_iterator: # iterate over dataframe
    c=next(color)
    sns.plt.plot(seg, color=c)

接下来,我将它们绘制为三维曲面:

# plot as a surface
surfacefig = plt.figure()
surfaceax = surfacefig.gca(projection='3d')
X = np.arange(LAGS+1)
Y = np.arange(num_segs)
X, Y = np.meshgrid(X, Y)
surfaceax.plot_surface(X, Y, df, cmap=plt.cm.Set2)
plt.show()

如何将颜色映射到行索引(而不是z值)?我想保留线条的颜色


更新结果:

# updated lines. Make sure XX and YY are floats
surf = surfaceax.plot_surface(XX, YY, df, shade=False,
             facecolors=plt.cm.Set2((YY-YY.min()) / (YY.max()-YY.min())), 
             cstride=1, rstride=5, alpha=0.7)
plt.draw() # you need this to get the edge color
line = np.array(surf.get_edgecolor())
surf.set_edgecolor(line*np.array([0,0,0,0])+1)
您可以尝试以下方法:

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

X = np.linspace(-np.pi, np.pi, 200, endpoint=True)
Y = np.linspace(-np.pi, np.pi, 200, endpoint=True)
XX, YY = np.meshgrid(X,Y)
Z = np.cos(XX)*np.cos(YY)

fig = plt.figure()
ax1 = plt.subplot2grid((1,2), (0,0), projection='3d')
ax2 = plt.subplot2grid((1,2), (0,1), projection='3d')
surf = ax1.plot_surface(XX, YY, Z,
                        cmap=plt.cm.Set2)
surf2 = ax2.plot_surface(XX, YY, Z, shade=False,
                         facecolors=plt.cm.Set2((XX-XX.min())/(XX.max()-XX.min()))
                         )
其中,在第二个绘图中,您将
facecolors
设置为XX的函数,而不是默认情况下的Z。您需要在0和1之间重新缩放XX值,否则
colormap
将在0和1之外饱和。您还需要删除在使用cmap(在第一个绘图中)时删除的阴影

然而,由于一些未知的原因,这些线消失了

您可以使用以下方法将其添加回:

plt.draw() # you need this to get the edge color
lines = np.array(surf2.get_edgecolor())
surf2.set_edgecolor(lines*np.array([0,0,0,0])+1) # make lines white, and keep alpha==1. It's an array of colors like this: [r,g,b,alpha]
它给出: