将matplotlib图形转换为相同形状的numpy数组 [英] Convert matplotlib figure to numpy array of same shape

查看:478
本文介绍了将matplotlib图形转换为相同形状的numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个256x256的图片,我希望能够绘制通过这些点的回归线。为此,我将图像转换为散点图,然后尝试将散点图转换回numpy数组。但是,转换回numpy数组会使numpy数组变为480x640。

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?

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

输出:(256L,256L)

Out: (256L, 256L)

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

输出:(480L,640L,4L)

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

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

输出:(640升,480升,4升)

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

谢谢

推荐答案

如果您可以在不设置参数figsize的情况下调用 matplotlib.pyplot.figure 呈现默认形状(引用文档中的内容):

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


figsize :(浮动,浮动),可选,默认:无宽度,高度以
英寸为单位。如果未提供,则默认为rcParams [ figure.figsize] =
[6.4,4.8]。

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

您可以通过

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

我不知道您的数据是什么样子,我认为您的方法相当round回,所以,我建议这样: p>

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()

链接到np.polyfit的文档

这篇关于将matplotlib图形转换为相同形状的numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆