球体Python上的密度图

Woo*_*ggy 2 python matplotlib matplotlib-basemap

如果我有一个与球坐标中给定的(theta,phi)点对应的数值数组,如何在球体表面上绘制密度图?我已经找到了如何构建一个球体,例如布洛赫球体在球体绘图.第一个例子非常好看 - 需要轴和热图.

wil*_*ill 6

如果你继承类的BlochQuTip,并改变它绘制球体的方式,你可以绘制密度图并保留它创建的所有其他框架.

matplotlib surface_plot示例为例,改变Bloch类的绘图功能.将它放在你自己的子类中可以防止你破坏库.

from qutip import Bloch
from math import sqrt, sin, cos, pi
from colorsys import hsv_to_rgb


from numpy import linspace, outer, ones, sin, cos, arccos, arctan2, size, empty
class BlochDensity(Bloch):
  def plot_back(self):
    # back half of sphere
    u = linspace(0, pi, 25)
    v = linspace(0, pi, 25)
    x = outer(cos(u), sin(v))
    y = outer(sin(u), sin(v))
    z = outer(ones(size(u)), cos(v))

    colours = empty(x.shape, dtype=object)
    for i in range(len(x)):
      for j in range(len(y)):
        theta = arctan2(y[i,j], x[i,j])
        phi = arccos(z[i,j])

        colours[i,j] = self.density(theta, phi)


    self.axes.plot_surface(x, y, z, rstride=1, cstride=1,
                           facecolors=colours,
                           alpha=self.sphere_alpha, 
                           linewidth=0, antialiased=True)
    # wireframe
    self.axes.plot_wireframe(x, y, z, rstride=5, cstride=5,
                             color=self.frame_color,
                             alpha=self.frame_alpha)
    # equator
    self.axes.plot(1.0 * cos(u), 1.0 * sin(u), zs=0, zdir='z',
                   lw=self.frame_width, color=self.frame_color)
    self.axes.plot(1.0 * cos(u), 1.0 * sin(u), zs=0, zdir='x',
                   lw=self.frame_width, color=self.frame_color)



  def plot_front(self):
    # front half of sphere
    u = linspace(-pi, 0, 25)
    v = linspace(0, pi, 25)
    x = outer(cos(u), sin(v))
    y = outer(sin(u), sin(v))
    z = outer(ones(size(u)), cos(v))

    colours = empty(x.shape, dtype=object)
    for i in range(len(x)):
      for j in range(len(y)):
        theta = arctan2(y[i,j], x[i,j])
        phi = arccos(z[i,j])

        colours[i,j] = self.density(theta, phi)


    self.axes.plot_surface(x, y, z, rstride=1, cstride=1,
                           facecolors=colours,
                           alpha=self.sphere_alpha, 
                           linewidth=0, antialiased=True)


    # wireframe
    self.axes.plot_wireframe(x, y, z, rstride=5, cstride=5,
                             color=self.frame_color,
                             alpha=self.frame_alpha)
    # equator
    self.axes.plot(1.0 * cos(u), 1.0 * sin(u),
                   zs=0, zdir='z', lw=self.frame_width,
                   color=self.frame_color)
    self.axes.plot(1.0 * cos(u), 1.0 * sin(u),
                   zs=0, zdir='x', lw=self.frame_width,
                   color=self.frame_color)
Run Code Online (Sandbox Code Playgroud)

我在这里做的是让绘图部分调用以下函数BlochDensity:self.density(theta, phi)- 我没有定义.

创建BlochDensity对象后,您需要创建该功能,这是theta, phi密度的映射.我建议使用SciPy的2D插值来创建函数,如下所示:

from scipy.interpolate import interp2d
from numpy.random import rand

b = BlochDensity()
b.sphere_alpha=0.5

thetas, phis = linspace(-pi,pi,10), linspace(0,pi,10)
density = rand(len(thetas), len(phis))

#scale density to a maximum of 1
density /= density.max()

interpolated_density = interp2d(thetas, phis, density)

def f(theta, phi):
  return hsv_to_rgb(interpolated_density(theta,phi), 1, 1)

b.density = f

b.show()

b.density = f

b.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果要增加分辨率,只需更改plot_*函数内的linspace中的数字即可BlochDensity.