繁体   English   中英

球体Python上的密度图

[英]density plot on a sphere Python

如果我在球坐标系中具有对应于给定(theta,phi)点的值数组,如何在球体表面绘制密度图? 我发现了如何构造一个球体,例如Bloch球体在球体 绘图 第一个示例看起来非常好-需要轴和热图。

如果您QuTipBloch类的子类,并更改其绘制球体的方式,则可以绘制密度图并保留它创建的所有其他框架。

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)

我在这里所做的是使绘图部分调用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()

在此处输入图片说明

如果要提高分辨率,则只需在plot_*函数内的linspace中更改数字BlochDensity

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM