Python 3.x 使用Matplotlib imshow meshgrid和自定义颜色创建图像

Python 3.x 使用Matplotlib imshow meshgrid和自定义颜色创建图像,python-3.x,numpy,matplotlib,Python 3.x,Numpy,Matplotlib,我试图创建一个图像,其中x轴是图像的宽度,y轴是图像的高度。其中每个点都可以根据RBG映射指定颜色。通过查看Matplotlib中的imshow(),我想我需要在表单(NxMx3)上创建一个网格网格,其中3是一个元组或类似的rbg颜色 但到目前为止,我还不明白如何做到这一点。假设我有一个例子: import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColorm

我试图创建一个图像,其中x轴是图像的宽度,y轴是图像的高度。其中每个点都可以根据RBG映射指定颜色。通过查看Matplotlib中的imshow(),我想我需要在表单(NxMx3)上创建一个网格网格,其中3是一个元组或类似的rbg颜色

但到目前为止,我还不明白如何做到这一点。假设我有一个例子:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

x_min = 1 
x_max = 5
y_min = 1 
y_max = 5
Nx = 5 #number of steps for x axis
Ny = 5 #number of steps for y axis

x = np.linspace(x_min, x_max, Nx)
y = np.linspace(y_min, y_max, Ny)

#Can then create a meshgrid using this to get the x and y axis system
xx, yy = np.meshgrid(x, y)

#imagine I have some funcion that does someting based on the x and y values
def somefunc(x_value, y_value):
    #do something and return rbg based on that
    return x_value + y_value

res = somefunc(xx, yy)

cmap = LinearSegmentedColormap.from_list('mycmap', ['white', 'blue', 'black'])
plt.figure(dpi=100)
plt.imshow(res, cmap=cmap, interpolation='bilinear')

plt.show()
这就创建了一个图,但是如果我的目标是基于somefunc中的x和y值给出specsific rbg值,并将得到的numpy数组变成nxmx3数组,我该怎么做呢


我试图让somefunc函数返回一个要使用的rbg值元组(r,bg),但这似乎不起作用,它当然完全取决于您要对提供给函数的值执行什么操作。假设你只想把x值作为红色通道,把y值作为蓝色通道,这看起来像

def somefunc(x_value, y_value):
    return np.dstack((x_value/5., np.zeros_like(x_value), y_value/5.))
完整示例:

import numpy as np
import matplotlib.pyplot as plt

x_min = 1 
x_max = 5
y_min = 1 
y_max = 5
Nx = 5 #number of steps for x axis
Ny = 5 #number of steps for y axis

x = np.linspace(x_min, x_max, Nx)
y = np.linspace(y_min, y_max, Ny)

#Can then create a meshgrid using this to get the x and y axis system
xx, yy = np.meshgrid(x, y)

#imagine I have some funcion that does someting based on the x and y values
def somefunc(x_value, y_value):
    return np.dstack((x_value/5., np.zeros_like(x_value), y_value/5.))


res = somefunc(xx, yy)

plt.figure(dpi=100)
plt.imshow(res)

plt.show()

如果您已经有一个(更复杂的)返回RGB元组的函数,您可以在网格上循环,用该函数的值填充空数组

#If you already have some function that returns an RGB tuple
def somefunc(x_value, y_value):
    if x_value > 2 and y_value < 3:
        return np.array(((y_value+1)/4., (y_value+2)/5., 0.43))
    elif x_value <=2:
        return np.array((y_value/5., (x_value+3)/5., 0.0))
    else:
        return np.array((x_value/5., (y_value+5)/10., 0.89))
# you may loop over the grid to fill a new array with those values
res = np.zeros((xx.shape[0],xx.shape[1],3))
for i in range(xx.shape[0]):
    for j in range(xx.shape[1]):
        res[i,j,:] = somefunc(xx[i,j],yy[i,j])



plt.figure(dpi=100)
plt.imshow(res)
#如果您已经有了返回RGB元组的函数
def somefunc(x_值,y_值):
如果x_值大于2,y_值小于3:
返回np.数组(((y_值+1)/4.,(y_值+2)/5.,0.43))

elif x_值我在理解这里的dstack函数时遇到了一些问题。还有什么可以放回数组的规则吗?例如,如果我确实计算了其他东西,而不是np.zero_like(x_值),比如说数字0.4,那么我得到“ValueError:除了连接轴之外的所有输入数组维度都必须完全匹配”是否有一些规则规定所有内容都必须是某种类型的numpy值?返回的数组需要是形状(n,m,3)或者(n,m,4),在你的例子中,n=Ny,m=Nx。您完全可以自由选择如何创建此阵列。在这里的解决方案中,我只是将3个(n,m)阵列堆叠在一起,得到一个(n,m,3)形状的阵列。假设somefunc被修改了一点,如果x+y大于5,则表示一种颜色,如果不是另一种颜色。您将如何构造该数组。例如,如果somefunc函数在(xx)范围内有某种形式的for i,那么这种情况会发生什么变化。我是否仍然可以使用np.vectorize,然后在网格上运行它?对于“如果x+y大于5…”的情况,您根本不会创建图像。相反,您只需使用一个colormap,
plt.imshow(xx+yy>5,cmap=matplotlib.colors.ListedColormap([“蓝色”、“粉色”))
来绘制二进制数组<代码>对于范围(xx)中的i
没有意义,因此我不确定应该给出什么,但您可以确定定义一些函数,使用
矢量化
将其应用于数组的每个元素。我不确定是否应该编辑我的第一篇文章或其他内容来显示我正在考虑的函数类型的示例。所以我在这里分享了一个代码示例,为什么即使在我将其矢量化之后,该函数也不能工作呢?