我正在尝试使用 Qutip 的函数 bloch() 在 bloch 球体上绘制实时数据。
到目前为止,当我有 b.show() 时,代码总是会中断。
我在网上找到了很多类似问题的解决方案,但大多数都使用直接的 matplotlib 命令,例如 matplotlib.draw() ,它似乎不适用于 bloch 类。 然后,还有其他使用 Tk 或 GTKagg 等解决方案(例如 https://stackoverflow.com/a/15742183/3276735 或 real-time plotting in while loop with matplotlib )
有人可以帮助我如何处理 bloch 类中的相同问题吗?
编辑: 这是一个最小的例子:
基本上,我想一次更新一个点,最好是循环更新我的绘图。我的目标是在图中显示必须从文件中读取的实时数据。
import qutip as qt
import numpy as np
b = qt.Bloch()
theta = np.arange(0,np.pi,0.1)
for ii in range(len(theta)):
b.add_points([np.sin(theta[ii]),0,np.cos(theta[ii])])
b.show()
最佳答案
我认为你正在破坏你的情节,因为你正在为每一点调用 show 。尝试在循环外(最后)调用 show。
import qutip as qt
import numpy as np
b = qt.Bloch()
theta = np.arange(0,np.pi,0.1)
for ii in range(len(theta)):
b.add_points([np.sin(theta[ii]),0,np.cos(theta[ii])])
b.show() # Changed here
编辑: 动画情节
将 show
视为调用绘图的绝对命令。它不是绘制命令(或重绘)。如果您确实想每隔“n”秒左右显示一张图像,则需要在再次调用之前清除
绘图。你可以试试这个:
import qutip as qt
import numpy as np
b = qt.Bloch()
theta = np.arange(0,np.pi,0.1)
for ii in range(len(theta)):
b.clear()
b.add_points([np.sin(theta[ii]),0,np.cos(theta[ii])])
b.show()
# wait time step and load new value from file.
,我当前的发行版中没有 QuTip,所以我无法真正测试它,但我敢打赌它很大程度上基于 matplotlib。然而,我最好的建议是您使用 QuTiP 中给出的动画公式。文档。按照这个食谱:
from pylab import *
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
fig = figure()
ax = Axes3D(fig,azim=-40,elev=30)
sphere=Bloch(axes=ax)
def animate(i):
sphere.clear()
sphere.add_vectors([sin(theta),0,cos(theta)])
sphere.add_points([sx[:i+1],sy[:i+1],sz[:i+1]])
sphere.make_sphere()
return ax
def init():
sphere.vector_color = ['r']
return ax
ani = animation.FuncAnimation(fig, animate, np.arange(len(sx)),
init_func=init, blit=True, repeat=False)
ani.save('bloch_sphere.mp4', fps=20, clear_temp=True)
,您应该能够修改 animate 函数来执行您需要的所有操作。
关于python - 布洛赫球上的实时绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184507/