python

时间:2015-09-05 13:03:39

标签: python matplotlib plot

我正在使用以下线绘制3D表面:

surf = ax3.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.5, linewidth=0, cmap=cm.jet,antialiased=True)

现在颜色非常好,虽然有点鳞状外观,虽然很好 但我想改变表面颜色w.r.t.另一个数据,存储在list中:

m = [104.48, 111.73,109.93,139.95,95.05,150.49,136.96,157.75]

我正在尝试:

norm = cls.Normalize() # Norm to map the 'm' values to [0,1]
norm.autoscale(m)
cmap = cm.ScalarMappable(norm, 'jet')
surf = ax3.plot_surface(X, Y, Z, rstride=5, cstride=5, alpha=0.5, linewidth=0, color=cmap.to_rgba(m), antialiased=True)

但是这会引发错误,因为cmap.to_rgba仅使用1D数组。 关于如何能够更改曲面colormap的任何建议都将受到高度赞赏。

3 个答案:

答案 0 :(得分:4)

嗯,它看起来很糟糕,但我认为你可以适应它:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
my_col = cm.jet(np.random.rand(Z.shape[0],Z.shape[1]))

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors = my_col,
        linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)

我不会使用jet,而是使用像cubehelix这样的线性色图。您可以使用错误的色彩图(one of many posts关于该主题)轻松欺骗眼睛

答案 1 :(得分:2)

要获得正确的颜色,请使用Z值从颜色贴图中选择值:

my_col = cm.jet(Z/np.amax(Z))

结果:

surface plot

使用与@Moritz相同的代码。

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
my_col = cm.jet(Z/np.amax(Z))

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors = my_col,
        linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)

plt.show()

答案 2 :(得分:0)

我使用PANDAS在python中使用了一些行,这个情节是美丽的!

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import pandas as pd
from sys import argv

file = argv[1]

x,y,z = np.loadtxt(file, unpack=True)
df = pd.DataFrame({'x': x, 'y': y, 'z': z})

fig = plt.figure()
ax = Axes3D(fig)
surf = ax.plot_trisurf(df.x, df.y, df.z, cmap=cm.jet, linewidth=0.1)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.savefig('teste.pdf')
plt.show()

Collapsing wave equations

更漂亮一点!在我的例子中,我使用了颜色图JET Colormaps Matplotlib,但还有其他种类的颜色和定性图。看看之前的链接。