【问题标题】:Live plotting on bloch sphere布洛赫球体上的实时绘图
【发布时间】:2016-07-11 02:49:54
【问题描述】:

我正在尝试使用 Qutip 的函数 bloch() 在 bloch 球体上绘制实时数据。

到目前为止,当我在其中有 b.show() 时,代码总是会中断。

我在网上找到了很多类似问题的解决方案,但大多数都使用直接的 matplotlib 命令,如 matplotlib.draw() 似乎不适用于 bloch 类。 然后,还有其他使用 Tk 或 GTKagg 的解决方案(例如 https://stackoverflow.com/a/15742183/3276735real-time plotting in while loop with matplotlib

有人可以帮我解决布洛赫课程中的同样问题吗?

编辑: 这是一个最小的例子:

基本上,我想一次更新我的情节,最好是循环更新。我的目标是在必须从文件中读取的图中显示实时数据。

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()

【问题讨论】:

  • 在此处添加您的代码的最小示例(如果可能,可以通过复制粘贴进行测试)。
  • 好的。我希望这样可以更容易地解决这个问题。

标签: python matplotlib plot qutip


【解决方案1】:

我认为你正在打破你的阴谋,因为你在为每一点都打电话。尝试在循环外调用 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”秒左右显示一张图像,则需要在再次调用该图之前clear。你可以试试这个:

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 函数以执行您需要的所有操作。

【讨论】:

  • 嗨@armatita,感谢您的回复。我的问题是我想显示我想在图中看到的实时数据。因此,我不能将所有内容都放入一个数组并立即显示所有内容。所以会有一个每 0.2 秒更新一次的文件,我想将最后一行读入 python 并将其绘制到图表中,以实时查看测量结果如何演变。这就是为什么我希望能够从循环内部进行绘图。
  • @Mechanix 我已经更新了我的帖子。我的回答依赖于我认为 QuTip 可能很像 matplotlib 的事实。如果它不起作用,我建议您只使用网站中提供的动画配方。帖子中的链接。
  • 再次感谢。问题仍然是我如何在屏幕上显示动画内容......对此有什么想法吗?
  • @Mechanix 该配方基于 matplotlib,可在屏幕上显示实时动画。您只需要根据您的案例研究对其进行调整。在此链接中,您将看到另一个使用实时随机生成数据的类似动画。请注意如何使用相同的调用:matplotlib.org/examples/animation/strip_chart_demo.html
  • 好吧,这很有趣,我实际上在看同一个例子。问题是,他们使用支持实时更新的 plt.draw()。就我而言,我只收到消息“AttributeError: Bloch instance has no attribute 'draw'”
猜你喜欢
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多