将颜色贴图应用于Matplotlib 3D曲面

时间:2015-05-28 05:15:32

标签: python matplotlib surface mplot3d colormap

我有时间序列数据,我已将其分成数百个块。我解决了每个片段的自相关并绘制了它们:

# 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)

Many autocorrelations

接下来,我将它们绘制为3D表面:

# 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()

3D surface

如何将颜色映射到行索引(而不是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)

Final figure

1 个答案:

答案 0 :(得分:3)

你可以试试这个:

select column1, column2, column3
from 
Table1 a
inner join 
Table2 b on a.column1=b.column1
where Condition1 and condition2 and not condition 3

在第二个图中,您将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())) ) 设置为XX的函数,而不是默认情况下的Z.您需要在0和1之间重新缩放XX值,否则facecolors将在0和1之外饱和。您还需要删除使用cmap时删除的阴影(在第一个图中)。

然而,由于某些未知原因,这些线条消失了。

您可以将它们添加回来:

colormap

它给出: cmap3d

HTH