Matplotlib 3d 绘图:在 2 个表面上获取单个颜色图 [英] Matplotlib 3d plot: get single colormap across 2 surfaces

查看:36
本文介绍了Matplotlib 3d 绘图:在 2 个表面上获取单个颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 2 个表面的 matplotlib 制作 3d 绘图(请参见下面的示例).到目前为止,两个表面都有自己的颜色图,底部为蓝色,顶部为黄色.

I am making a 3d plot with matplotlib with 2 surfaces (see example below). So far both surfaces get their own colormap, being blue in the bottom and yellow on top.

但是,我想要两个表面的单一颜色图,即最底部为蓝色,最顶部为黄色,两个表面的接触点为绿色.

However, I want a single colormap for both surfaces, i.e. the very bottom is blue, the very top is yellow and the touching point of both surfaces is green.

我怎样才能做到这一点?我需要在绘图之前以某种方式组合两个表面,还是需要限制两个表面的颜色图(从蓝色到绿色,从绿色到黄色)?

How can I achieve that? Do I need to somehow combine both surfaces before plotting, or do I need to restrict the colormaps of both surfaces (lower from blue to green, upper from green to yellow)?

感谢您的帮助.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm

ky = np.linspace(-np.pi*2/3,np.pi*2/3,100)
kz = np.linspace(-np.pi*2/3,np.pi*2/3,100)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

KY, KZ = np.meshgrid(ky, kz)
E = np.cos(KY)*np.cos(KZ)
ax.plot_surface(KY, KZ, E-1, rstride=1, cstride=1, cmap=cm.viridis)   #surface 1
ax.plot_surface(KY, KZ, -E+1, rstride=1, cstride=1, cmap=cm.viridis)  #surface 2
ax.view_init(elev=7, azim=-69)
plt.show()

推荐答案

您可以为颜色图显式设置 vminvmax 以强制颜色范围.

You can explicitly set vmin and vmax for a colormap to force the range for the colors.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm

ky = np.linspace(-np.pi*2/3,np.pi*2/3,100)
kz = np.linspace(-np.pi*2/3,np.pi*2/3,100)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

KY, KZ = np.meshgrid(ky, kz)
E = np.cos(KY)*np.cos(KZ)
ax.plot_surface(KY, KZ, E-1, rstride=1, cstride=1, cmap=cm.viridis, vmin=-2, vmax=2)   #surface 1
ax.plot_surface(KY, KZ, -E+1, rstride=1, cstride=1, cmap=cm.viridis, vmin=-2, vmax=2)  #surface 2
ax.view_init(elev=7, azim=-69)
plt.show()

要使范围与两个曲面中的实际 Z 值紧密相关,您可以使用

To make the range tight against actual Z values in your two surfaces, you can use

vmin=np.amin(E-1), vmax=np.amax(-E+1)

您还可以通过定义自己的颜色映射来创建这种效果,这些颜色映射在顶部从黄色变为绿色,在底部从绿色变为蓝色.

You could also create this effect by defining your own color maps that go from yellow to green at the top, and from green to blue at the bottom.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
from matplotlib.colors import ListedColormap

ky = np.linspace(-np.pi*2/3,np.pi*2/3,100)
kz = np.linspace(-np.pi*2/3,np.pi*2/3,100)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

KY, KZ = np.meshgrid(ky, kz)
E = np.cos(KY)*np.cos(KZ)

viridis = cm.get_cmap('viridis', 512)
topcolors = viridis(np.linspace(0.5, 1, 256))
topcm = ListedColormap(topcolors)
bottomcolors = viridis(np.linspace(0, 0.5, 256))
bottomcm = ListedColormap(bottomcolors)

ax.plot_surface(KY, KZ, E-1, rstride=1, cstride=1, cmap=bottomcm)   #surface 1
ax.plot_surface(KY, KZ, -E+1, rstride=1, cstride=1, cmap=topcm)  #surface 2
ax.view_init(elev=7, azim=-69)
plt.show()

这篇关于Matplotlib 3d 绘图:在 2 个表面上获取单个颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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