如何在python中将3D函数绘制为2D颜色图? [英] How to plot 3D function as 2D colormap in python?
本文介绍了如何在python中将3D函数绘制为2D颜色图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何python库可让我绘制z = f(x,y),其中z表示为密集光栅化图像中的颜色(与一堆散点图的颜色相反)?如果是这样,我应该使用什么功能?
Are there any python libraries that will let me plot z = f(x,y) where z is represented as the color in a densely rasterized image (as opposed to the color of a bunch of scatterplot points) ? If so, what function do I use?
看起来matplotlib.pyplot中的某些轮廓函数接近我想要的轮廓,但是它们绘制轮廓线,而我不希望那样.
It looks like some of the contour functions in matplotlib.pyplot come close to what I want, but they draw contour lines and I don't want that.
推荐答案
这是一个具体的简单示例(也适用于不能接受x
和y
的矩阵参数的函数):
here's a concrete simple example (works also for functions which can't take matrix arguments for x
and y
):
# 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中将3D函数绘制为2D颜色图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文