Python中3D表面图的颜色 [英] color of a 3D surface plot in python

查看:1395
本文介绍了Python中3D表面图的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下行绘制3D曲面:

I'm using following line for plotting a 3D surface:

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中为:

Now the color comes very nice, although a bit scaly appearance, though fine.
But I want to change the surface color w.r.t. another data, stored in list as:

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仅采用一维数组. 任何有关如何更改表面colormap的建议将不胜感激.

But this is raising an error as cmap.to_rgba takes 1D arrays only. Any suggestions on how can I be able to change the colormap of the surface would be highly appreciated.

推荐答案

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

Well, it looks awful but I think you can adapt it:

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)

我不会使用喷射,而是使用一些线性色彩图,例如cubehelix.您可以使用错误的颜色图轻松欺骗眼睛(有关该主题的众多帖子之一)

I would not use jet but some linear colormap like cubehelix. You can trick the eye easily using the wrong colormap (one of many posts on that topic)

这篇关于Python中3D表面图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆