简体   繁体   中英

How to choose which figure on which to display an image with matplotlib

I have a python script with multiple figures that I would like to update during a loop. Some will be images, and others will be line/scatter plots. I am having trouble getting the image to display on the correct figure. (the line and scatter data are showing up on the right figures, but the image seems to always be going on the figure that was created last, eventually I'll be displaying more than one image figure, so I can't just create the image figure last)

Here is roughly the code I have so far, the 3D scatter plot is showing up on Figure 1, but both the image and the line plots are showing up on Figure 3, with Figure 2 blank:

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

Basically I have some queues that get populated in a callback when new data arrives, and I want this data to be displayed as it arrives.

I am new to matplotlib, so any help with my image display issue or tips for better ways of using matplotlib to display figures in general will be much appreciated

You are mixing the OO and state machine interfaces. See this answer for an explanation of what is going on.

Replace this line:

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

with

the_axes_you_want.imshow(...)

which should fix your image issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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