@brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it @param fig a matplotlib figure @return a numpy 3D array of RGBA values
(fig)
| 240 | |
| 241 | @staticmethod |
| 242 | def fig2data(fig): |
| 243 | """ |
| 244 | @brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it |
| 245 | @param fig a matplotlib figure |
| 246 | @return a numpy 3D array of RGBA values |
| 247 | """ |
| 248 | # draw the renderer |
| 249 | fig.canvas.draw() |
| 250 | |
| 251 | # Get the RGBA buffer from the figure |
| 252 | w, h = fig.canvas.get_width_height() |
| 253 | buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8) |
| 254 | buf.shape = (w, h, 4) |
| 255 | |
| 256 | # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode |
| 257 | buf = np.roll(buf, 3, axis=2) |
| 258 | return buf.reshape(h, w, 4) |
| 259 | |
| 260 | @staticmethod |
| 261 | def imfrombytes(content, flag='color'): |