0

I want to read current view of pyplot figure as numpy array without saving the file and loading it.

What I tried first was to plot the data and save it and load it using imread. But I guess this save-and-load reduces task's time-efficiency.

# Plotting
plt.plot(data_x, data_y, data_z)
plt.savefile('output.png')
plt.close()

# Load image as numpy array
im = cv2.read('output.png')

How can I change this code to do same work but increase time efficiency?

Soyong
  • 63
  • 1
  • 3

1 Answers1

0

Sorry to bother you. My code is to see 3D skeletal joints model (each neighbor joints are connected as line) in 3D coordinate.

import matplotlib.pyplot as plt
import numpy as np


def draw_3D_model(file_name)
   fig = plt.figure()
   ax = plt.axes(projection = '3d')
   ax.grid(False)
   ax.view_init(azim = -90,elev = -50)

   draw_skeleton(file_name)   # this is the function I made to draw a line between neighbor joints(only used plt.plot()

   return fig


def make_animation   
   for file_name in file_names:
      fig = draw_3D_model(file_name)
      fig.canvas.draw ( )
      w,h = fig.canvas.get_width_height()
      buf = numpy.fromstring ( fig.canvas.tostring_argb(), dtype=numpy.uint8 )
      buf.shape = ( w, h,4 )

This is simplified structure of my code, and when doing buf.shape = ( w, h, 4 ), it shows the error message that the size of buf cannot be reshape to ( w, h, 4 ).

Soyong
  • 63
  • 1
  • 3