【问题标题】:How to choose which figure on which to display an image with matplotlib如何使用 matplotlib 选择在哪个图形上显示图像
【发布时间】:2013-06-06 19:47:59
【问题描述】:

我有一个包含多个图形的 python 脚本,我想在循环期间更新它们。有些是图像,有些是线/散点图。我无法让图像显示在正确的图形上。 (线和散点数据显示在正确的图形上,但图像似乎总是在最后创建的图形上,最终我将显示多个图像图形,所以我不能只创建图片最后)

这是我目前的大致代码,3D 散点图显示在图 1 中,但图像和线图都显示在图 3 中,图 2 为空白:

import matplotlib.pyplot as plt
from collections import deque

class Bla():

  def __init__( self ):

    self.pc_fig = plt.figure(1)
    self.pc_ax = self.pc_fig.add_subplot(111, projection='3d')
    self.pc_ax.set_xlim3d([0, 50])
    self.pc_ax.set_ylim3d([0, 50])
    self.pc_ax.set_zlim3d([0, 20])
    self.pc_ax.hold(False)

    self.vts_fig = plt.figure(2)
    self.vts_ax = self.vts_fig.add_subplot(111)

    self.em_fig = plt.figure(3)
    self.em_ax = self.em_fig.add_subplot(111)
    self.em_ax.hold(True)

    self.image_data = deque()
    self.motion_data = deque()

    plt.ion()
    plt.show()

  def run( self ):

    em_prev_xy = ( 0, 0 )
    while True:
      if len( self.motion_data ) > 0:
        data1 = self.motion_data.popleft()
        em_xy = data1.get_em()
        self.em_ax.plot( [ em_prev_xy[0], em_xy[0] ], [ em_prev_xy[1], em_xy[1] ],'b')
        pc = self.get_pc()
        pc_index = nonzero(pc>.002)
        pc_value = pc[pc_index] * 100
        self.pc_ax.scatter(pc_index[0],pc_index[1],pc_index[2],s=pc_value)
        self.pc_ax.set_xlim3d([0, 50])
        self.pc_ax.set_ylim3d([0, 50])
        self.pc_ax.set_zlim3d([0, 20])
        plt.pause( 0.0001 ) # This is needed for the display to update
      if len( self.image_data ) > 0:
        im = self.image_data.popleft()
        plt.imshow( im, cmap=plt.cm.gray, axes=self.vts_ax )
        plt.pause( 0.0001 )

def main():
  bla = Bla()
  bla.run()

if __name__ == "__main__":
  main()

基本上,当新数据到达时,我有一些队列会在回调中填充,我希望这些数据在到达时显示出来。

我是 matplotlib 的新手,因此对于我的图像显示问题的任何帮助或关于使用 matplotlib 显示一般数字的更好方法的提示将不胜感激

【问题讨论】:

    标签: image python-2.7 matplotlib figure


    【解决方案1】:

    您正在混合使用 OO 和状态机接口。请参阅 this answer 了解正在发生的事情。

    替换这一行:

    plt.imshow( im, cmap=plt.cm.gray, axes=self.vts_ax )
    

    the_axes_you_want.imshow(...)
    

    这应该可以解决您的图像问题。

    【讨论】:

    • 谢谢!现在完美运行
    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2020-09-16
    • 2018-04-07
    相关资源
    最近更新 更多