How to plot 3D function as 2D colormap in python?

后端 未结 4 687
半阙折子戏
半阙折子戏 2020-12-30 04:30

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 scat

4条回答
  •  悲&欢浪女
    2020-12-30 05:04

    To give credit where it's due: this is only a slight variation on Andre Holzner's answer. Please upvote him if you must!

    import pylab
    
    def f(x, y):
        return pylab.cos(x) + pylab.sin(y)
    
    xx = pylab.linspace(-5, 5, 100)
    yy = pylab.linspace(-5, 5, 100)
    zz = pylab.zeros([len(xx), len(yy)])
    
    for i in xrange(len(xx)):
        for j in xrange(len(yy)):
            zz[j, i] = f(xx[i], yy[j])
    
    pylab.pcolor(xx, yy, zz)
    pylab.show()
    

    The syntax is perhaps easier to read with the strict minimum of array dimensions and indices. It relies on the following point (quoted from the doc).

    If either or both of X and Y are 1-D arrays or column vectors, they will be expanded as needed into the appropriate 2-D arrays, making a rectangular grid.

提交回复
热议问题