import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
Sometimes we may want to create an image with plotting API of matplotlib, but then apply a transform that only works for numpy arrays (for example, skimage’s warps). This is how you do it:
Let’s generate an example chart:
= [1,2,3]
x = [10,15,12]
bars
= plt.subplots(dpi=100)
f, ax # when using plt.subplots, the canvas and renderer get created automatically
; ax.bar(x, bars)
Export the figure to a numpy array
Method 1 src
= f.canvas.print_to_buffer()
s, (width, height) = np.frombuffer(s, np.uint8).reshape((height, width, 4))
rgba ='equal'); plt.imshow(rgba, aspect
print(type(rgba))
print(rgba.shape)
<class 'numpy.ndarray'>
(400, 600, 4)
Method 2 src
= np.asarray(f.canvas.buffer_rgba())
rgba ; plt.imshow(rgba)
print(type(rgba))
print(rgba.shape)
<class 'numpy.ndarray'>
(400, 600, 4)
Remove the alpha channel in a numpy array (RGBA->RGB)
To remove the alpha channel it’s enough to run: src
= rgba[:,:,:3] rgb
The resulting chart is the same (our transparency is anyway white)
; plt.imshow(rgb)
Both methods respect the dpi
set when creating the figure using plt.subplots
Warping images using skimage
Now we can warp the image:
from skimage.transform import swirl
= swirl(rgb, rotation=0, strength=30, radius=120) swirled
= plt.subplots(ncols = 2, figsize=(10,4))
f, ax
0].imshow(rgb, aspect='equal')
ax[0].set_title('original')
ax[
1].imshow(swirled, aspect='equal')
ax[1].set_title('warped')
ax[
'off') for a in ax]; [a.axis(
More on skimage transforms: https://scikit-image.org/docs/stable/api/skimage.transform.html