Skip to content

Instantly share code, notes, and snippets.

@orm011
Last active August 9, 2024 10:37
Show Gist options
  • Save orm011/c674f55566fd83609ba6c41699acb728 to your computer and use it in GitHub Desktop.
Save orm011/c674f55566fd83609ba6c41699acb728 to your computer and use it in GitHub Desktop.
pyplot figure to numpy array
def plt2arr(fig, draw=True):
"""
need to draw if figure is not drawn yet
"""
if draw:
fig.canvas.draw()
rgba_buf = fig.canvas.buffer_rgba()
(w,h) = fig.canvas.get_width_height()
rgba_arr = np.frombuffer(rgba_buf, dtype=np.uint8).reshape((h,w,4))
return rgba_arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment