Painting a sphere surface with a set of scalar values ​​in matplotlib

I am new to matplotlib (and this is also my first question here). I am trying to imagine the surface potential of the scalp recorded by EEG. So far, I have a two-dimensional figure of the projection of the sphere, which I generated using the contour, and largely comes down to a normal heat map.

Can this be done in half the sphere ?, i.e. create a 3D sphere with surface colors defined by a list of values? Something like this http://embal.gforge.inria.fr/img/inverse.jpg , but I have more than enough with a half sphere.

I saw several related questions (for example, the color chart of Matplotlib - is this possible? ), But they either do not affect my question or remain unanswered to date.

I also spent the morning looking through countless examples. In most of what I found, the color at one particular point on the surface indicates its Z value, but I don’t want this ... I want to draw a surface, and then specify the colors with the data I have.

+3
source share
1 answer

You can use plot_trisurf and assign the custom field to the base ScalarMappable using the 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() 

enter image description here

+4
source

All Articles