问题描述
我对 matplotlib 比较陌生(这也是我在这里的第一个问题).我试图表示脑电图记录的头皮表面电位.到目前为止,我有一个球体投影的二维图形,它是我使用 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 color plot- 有可能吗?),但他们要么没有真正解决我的问题,要么至今仍未得到答复.
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 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.
推荐答案
您可以使用 plot_trisurf 并将自定义字段分配给底层 ScalarMappable 通过 set_array
方法.
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 中的一组标量值为球体表面着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!