如何在平面上绘制任意颜色函数?

时间:2017-10-02 14:59:27

标签: python matplotlib

在python中,假设我想编写一个python函数,取一个点(x,y)并返回一个颜色。在矩形上绘制该函数的最简单方法是什么?我已经有numpy和matplotlib,但我愿意安装任何其他免费库。根据经验,matplotlib似乎让它变得有点痛苦,我不想做任何比这更复杂的事情:

from library import plot

def f(x, y):
    # etc

plot(f, xmin=0, xmax=1, ymin=0, ymax=1)

1 个答案:

答案 0 :(得分:0)

以下是颜色函数f将坐标xy映射到RGB数组的示例。给定网格上的一些输入坐标,您可以评估f并将其显示为图像。

import numpy as np
import matplotlib.pyplot as plt

# color function, from input (x,y) in the range between 0 and 1
# return an RGB tuple
f = lambda x,y: np.stack((x,np.zeros_like(x),y), axis=2)

# input grid of values between 0 and 1
X,Y = np.meshgrid(np.linspace(0,1,51),np.linspace(0,1,51))
# plot function:
plt.imshow(f(X,Y))

plt.show()

enter image description here

相关问题