使用matplotlib中的一组标量值为球体表面着色 [英] Colouring the surface of a sphere with a set of scalar values in matplotlib

查看:268
本文介绍了使用matplotlib中的一组标量值为球体表面着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对matplotlib相当陌生(这也是我在这里的第一个问题).我试图代表由EEG记录的头皮表面电位.到目前为止,我有一个球投影的二维图形,它是使用contourf生成的,并且可以归结为一个普通的热图.

I am rather new to matplotlib (and this is also my first question here). I'm trying to represent the scalp surface potential as recorded by an EEG. So far I have a two-dimensional figure of a sphere projection, which I generated using contourf, and pretty much boils down to an ordinary heat map.

有没有办法在半球形上完成此操作?即生成具有由值列表指定的表面颜色的3D球形?像这样的东西, http://embal.gforge.inria.fr/img/inverse. jpg ,但仅用半个球就足够了.

Is there any way this can be done on half a sphere?, i.e. generating a 3D sphere with surface colours given by a list of values? Something like this, http://embal.gforge.inria.fr/img/inverse.jpg, but I have more than enough with just half a sphere.

我看到了一些相关的问题(例如, Matplotlib 3d彩色图-可以吗?),但是他们要么没有真正解决我的问题,要么至今仍未回答.

I have seen a few related questions (for example, Matplotlib 3d colour plot - is it possible?), but they either don't really address my question or remain unanswered to date.

我还花了整个上午的时间浏览无数的示例.在我发现的大多数内容中,表面某个特定点的颜色指示其Z值,但是我不希望那样...我想绘制表面,然后使用数据I指定颜色有.

I have also spent the morning looking through countless examples. In most of what I've found, the colour at one particular point of a surface is indicative of its Z value, but I don't want that... I want to draw the surface, then specify the colours with the data I have.

推荐答案

您可以使用

You can use plot_trisurf and assign a custom field to the underlying ScalarMappable through set_array method.

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

(n, m) = (250, 250)

# Meshing a unit sphere according to n, m 
theta = np.linspace(0, 2 * np.pi, num=n, endpoint=False)
phi = np.linspace(np.pi * (-0.5 + 1./(m+1)), np.pi*0.5, num=m, endpoint=False)
theta, phi = np.meshgrid(theta, phi)
theta, phi = theta.ravel(), phi.ravel()
theta = np.append(theta, [0.]) # Adding the north pole...
phi = np.append(phi, [np.pi*0.5])
mesh_x, mesh_y = ((np.pi*0.5 - phi)*np.cos(theta), (np.pi*0.5 - phi)*np.sin(theta))
triangles = mtri.Triangulation(mesh_x, mesh_y).triangles
x, y, z = np.cos(phi)*np.cos(theta), np.cos(phi)*np.sin(theta), np.sin(phi)

# Defining a custom color scalar field
vals = np.sin(6*phi) * np.sin(3*theta)
colors = np.mean(vals[triangles], axis=1)

# Plotting
fig = plt.figure()
ax = fig.gca(projection='3d')
cmap = plt.get_cmap('Blues')
triang = mtri.Triangulation(x, y, triangles)
collec = ax.plot_trisurf(triang, z, cmap=cmap, shade=False, linewidth=0.)
collec.set_array(colors)
collec.autoscale()
plt.show()

这篇关于使用matplotlib中的一组标量值为球体表面着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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