Emily
Emily

Reputation: 59

Convert matplotlib figure to numpy array of same shape

I have an 256x256 image and I want to be able to plot a regression line through the points. To do this I converted the image to a scatter plot and then tried to convert the scatter plot back to a numpy array. However, conversion back to a numpy array made the numpy array 480x640.

Would anyone please be able to explain to me why the shape changes, mainly why it's no longer a square image, and if there's any conversion to fix it?

Making my x and y points from binary image

imagetile = a[2]
x, y = np.where(imagetile>0)
imagetile.shape

Out: (256L, 256L)

Version 1

from numpy import polyfit
from numpy import polyval

imagetile = a[2]
x, y = np.where(imagetile>0)

from numpy import polyfit
from numpy import polyval

p2 = polyfit(x, y, 2)

fig = plt.figure()
ax = fig.add_axes([0.,0.,1.,1.])
xp = np.linspace(0, 256, 256)
plt.scatter(x, y)
plt.xlim(0,256)
plt.ylim(0,256)
plt.plot(xp, polyval(p2, xp), "b-")
plt.show()

fig.canvas.draw()
X = np.array(fig.canvas.renderer._renderer)
X.shape

Out: (480L, 640L, 4L)

Version 2

def fig2data ( fig ):
    """
    @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
    """
    # draw the renderer
    fig.canvas.draw ( )
 
    # Get the RGBA buffer from the figure
    w,h = fig.canvas.get_width_height()
    buf = np.fromstring ( fig.canvas.tostring_argb(), dtype=np.uint8 )
    buf.shape = ( w, h,4 )
 
    # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
    buf = np.roll ( buf, 3, axis = 2 )
    return buf

figure = matplotlib.pyplot.figure(  )
plot   = figure.add_subplot ( 111 )
 

x, y = np.where(imagetile>0)
p2 = polyfit(x, y, 2)
plt.scatter(x, y)
plt.xlim(0,256)
plt.ylim(0,256)
plt.plot(xp, polyval(p2, xp), "b-")

data = fig2data(figure)
data.shape

Out: (640L, 480L, 4L)

Thank you

Upvotes: 0

Views: 2533

Answers (1)

warped
warped

Reputation: 9481

If you call matplotlib.pyplot.figure without setting the argument figsize, it takes on a default shape (quote from the documentation):

figsize : (float, float), optional, default: None width, height in inches. If not provided, defaults to rcParams["figure.figsize"] = [6.4, 4.8].

So, you could set the shape by doing

matplotlib.pyplot.figure(figsize=(2.56,2.56))

Not knowing what your data looks like, I think your approach is rather roundabout, so, I suggest something like this:

import numpy as np
import matplotlib.pyplot as plt

# generating simulated polynomial data:
arr = np.zeros((256, 256))
par = [((a-128)**2, a) for a in range(256)]
par = [p for p in par if p[0]<255]
arr[zip(*par)] = 1

x, y = np.where(arr>0)
p2 = np.polyfit(y, x, 2)
xp = np.linspace(0,256,256)

plt.imshow(arr) # show the image, rather than the conversion to datapoints

p = np.poly1d(p2) # recommended in the documentation for np.polyfit

plt.plot(xp, p(xp))

plt.ylim(0,256)
plt.xlim(0,256)

plt.show()

link to the documentation of np.polyfit

Upvotes: 1

Related Questions