I want to get a matplotlib figure as a 3 dimensional RGBA array. I'm using the following code to do the conversion:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
canvas = np.zeros((20, 20))
img = plt.imshow(canvas, interpolation='none').make_image()
h, w, d = img.as_rgba_str()
print(h,w)
rgba_array = np.fromstring(d, dtype=np.uint8).reshape(h, w, 4)
plt.imshow(rgba_array)
Out[1]: (249, 373)
<matplotlib.image.AxesImage at 0x111fa8b10>
Why the aspect ratio changes from the original square array? Is there any parameter that I can specify or an alternative method to get the figure's rgba array in its original shape?