gpt4 book ai didi

python - 如何在 python 中将 3D 函数绘制为 2D 颜色图?

转载 作者:太空狗 更新时间:2023-10-29 17:05:24 25 4
gpt4 key购买 nike

是否有任何 python 库可以让我绘制 z = f(x,y),其中 z 表示为密集光栅化图像中的颜色(与一堆散点图点的颜色相反)?如果是这样,我使用什么功能?

看起来 matplotlib.pyplot 中的一些等高线函数接近我想要的,但它们绘制等高线,我不想要那样。

最佳答案

这是一个具体的简单示例(也适用于不能为 xy 采用矩阵参数的函数):

# the function to be plotted
def func(x,y):
# gives vertical color bars if x is horizontal axis
return x

import pylab

# define the grid over which the function should be plotted (xx and yy are matrices)
xx, yy = pylab.meshgrid(
pylab.linspace(-3,3, 101),
pylab.linspace(-3,3, 111))

# indexing of xx and yy (with the default value for the
# 'indexing' parameter of meshgrid(..) ) is as follows:
#
# first index (row index) is y coordinate index
# second index (column index) is x coordinate index
#
# as required by pcolor(..)

# fill a matrix with the function values
zz = pylab.zeros(xx.shape)
for i in range(xx.shape[0]):
for j in range(xx.shape[1]):
zz[i,j] = func(xx[i,j], yy[i,j])

# plot the calculated function values
pylab.pcolor(xx,yy,zz)

# and a color bar to show the correspondence between function value and color
pylab.colorbar()

pylab.show()

关于python - 如何在 python 中将 3D 函数绘制为 2D 颜色图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5186282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com