Matplotlib:用 alpha 绘制 3d 数据 [英] Matplotlib: Plot 3d data with alpha

查看:44
本文介绍了Matplotlib:用 alpha 绘制 3d 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3D 数据,即 data.shape=(N,N,N).我打算做的是在 3D 中绘制这些数据,使用不同的颜色来反映 data[x,y,z] 的值.如果某个值低于阈值,我希望该点完全不可见 (alpha=0).所以从这个意义上说,我正在寻找一个3D 等高线图".

I have 3D data, i.e. data.shape=(N,N,N). What I am planning to do is to plot this data in 3D, using different colors to reflect the value of data[x,y,z]. If some value is under a threshold I want this point to be fully invisible (alpha=0). So in this sense I am looking for a "3D contour plot".

这甚至可以用 matplotlib 实现吗?

Is this even possible with matplotlib?

感谢您的建议.

我在 mathematica 中使用 ListContourPlot3D 找到了一个解决方案.如果有 matplotlib 的等价物,请告诉我,谢谢

I have found a solution in mathematica using ListContourPlot3D. If there is an equivalent for matplotlib please let me know, thanks

推荐答案

当然有可能,请参见下面的示例,其中 3 个 nxm 网格彼此重叠绘制.请注意,中间的已屏蔽数据(在本例中)高于某个阈值.可以使用颜色图的 cmap.set_bad() 属性控制 alpha.

Its certainly possible, see the example below where 3 nxm grids are plotted above each other. Notice that the middle one has masked data (in this case) above a certain threshold. The alpha can be controlled by using the cmap.set_bad() property of a colormap.

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

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
fig.text(.5,.9, 'Fancy plot', ha='center', size=14)

ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])

cmap=plt.cm.RdYlGn
cmap.set_bad('white', alpha=0)

p = ax.pcolor(x, y, ndvi1data, cmap=cmap)
art3d.poly_collection_2d_to_3d(p, 0)

q = ax.pcolor(x, y, np.ma.masked_where(ndvi2data>100,ndvi2data), cmap=cmap)
art3d.poly_collection_2d_to_3d(q, 5)

o = ax.pcolor(x, y, ndvi3data, cmap=cmap)
art3d.poly_collection_2d_to_3d(o, 10)

ax.set_xlim([x[0,0], x[-1,0]])
ax.set_ylim([y[0,0], y[0,-1]])
ax.set_zlim([0,10])

fig.colorbar(p)

这篇关于Matplotlib:用 alpha 绘制 3d 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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