在matplotlib中,Matlab的surf(x,y,z,c)的等效值是多少?

时间:2022-05-13 23:45:15

I want to realize the function like surf(x,y,z,c) in matlab, here x,y and z are the coordinates, and c is a variable value, I can use c to define the color. I don't know how to realize it with matplotlib.

我想在matlab中实现像surf(x,y,z,c)这样的函数,这里x,y,z是坐标,c是一个变量值,我可以用c来定义颜色。我不知道如何用matplotlib实现它。

3 个解决方案

#1


4  

I've done it using code something like this (see Edgelines vanish in mplot3d surf when facecolors are specified):

我用代码做过这样的事情(当facecolors被指定时,在mplot3d surf中看到Edgelines消失):

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import matplotlib
from pylab import *
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')


#Create X and Y data
x = np.arange(xmin, xmax, xstep)
y = np.arange(ymin, ymax, ystep)
X, Y = np.meshgrid(x, y)


surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=C, antialiased=True)

#Show the plot
plt.show()

#2


3  

You need to create a scalar map which will convert the values stored in your array 'C' into color values:

您需要创建一个标量映射,它将把数组中存储的值转换为颜色值:

from matplotlib.colors import Normalize
from matplotlib import cm

import matplotlib.pyplot as plt

# assuming X, Y, Z, C are given in correct format
# Z and C have same dimensions

min = C.min()
max = C.max()

# choose any colormap e.g. cm.jet, cm.coolwarm, etc.
color_map = cm.RdYlGn # reverse the colormap: cm.RdYlGn_r
scalarMap = cm.ScalarMappable(norm=Normalize(vmin=min, vmax=max), cmap=color_map)

# outputs an array where each C value is replaced with a corresponding color value
C_colored = scalarMap.to_rgba(C)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=C_colored, antialiased=True)

#3


0  

Here's a convenience function combining the other two answers (https://*.com/a/22176126/171761 and https://*.com/a/23799389/171761 ), which allows you to pass in a single argument (a raster Z, like imshow) and a colormap, and computes X, Y, and C (or which allows you to pass in Z, the colormap, and some of X, Y, and C):

这是一个方便的函数结合其他两个答案(https://*.com/a/22176126/171761和https://*.com/a/23799389/171761),它允许您传递一个参数(光栅Z,就像imshow)和colormap,和计算X,Y,和C(或者你可以通过Z,colormap,和一些X,Y,和C):

def surf(Z, colormap, X=None, Y=None, C=None, shade=None):
    if X is None and Y is None:
        X, Y = meshgrid_of(Z)
    elif X is None:
        X, _ = meshgrid_of(Z)
    elif Y is None:
        _, Y = meshgrid_of(Z)

    if C is None:
        C = Z

    scalarMap = cm.ScalarMappable(norm=Normalize(vmin=C.min(), vmax=C.max()), cmap=colormap)

    # outputs an array where each C value is replaced with a corresponding color value
    C_colored = scalarMap.to_rgba(C)

    ax = gca(projection='3d')

    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=C_colored, shade=shade)

    return surf


def meshgrid_of(A):
    xx, yy = meshgrid(range(shape(A)[1]), range(shape(A)[0]))
    return xx, yy

#1


4  

I've done it using code something like this (see Edgelines vanish in mplot3d surf when facecolors are specified):

我用代码做过这样的事情(当facecolors被指定时,在mplot3d surf中看到Edgelines消失):

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import matplotlib
from pylab import *
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')


#Create X and Y data
x = np.arange(xmin, xmax, xstep)
y = np.arange(ymin, ymax, ystep)
X, Y = np.meshgrid(x, y)


surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=C, antialiased=True)

#Show the plot
plt.show()

#2


3  

You need to create a scalar map which will convert the values stored in your array 'C' into color values:

您需要创建一个标量映射,它将把数组中存储的值转换为颜色值:

from matplotlib.colors import Normalize
from matplotlib import cm

import matplotlib.pyplot as plt

# assuming X, Y, Z, C are given in correct format
# Z and C have same dimensions

min = C.min()
max = C.max()

# choose any colormap e.g. cm.jet, cm.coolwarm, etc.
color_map = cm.RdYlGn # reverse the colormap: cm.RdYlGn_r
scalarMap = cm.ScalarMappable(norm=Normalize(vmin=min, vmax=max), cmap=color_map)

# outputs an array where each C value is replaced with a corresponding color value
C_colored = scalarMap.to_rgba(C)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=C_colored, antialiased=True)

#3


0  

Here's a convenience function combining the other two answers (https://*.com/a/22176126/171761 and https://*.com/a/23799389/171761 ), which allows you to pass in a single argument (a raster Z, like imshow) and a colormap, and computes X, Y, and C (or which allows you to pass in Z, the colormap, and some of X, Y, and C):

这是一个方便的函数结合其他两个答案(https://*.com/a/22176126/171761和https://*.com/a/23799389/171761),它允许您传递一个参数(光栅Z,就像imshow)和colormap,和计算X,Y,和C(或者你可以通过Z,colormap,和一些X,Y,和C):

def surf(Z, colormap, X=None, Y=None, C=None, shade=None):
    if X is None and Y is None:
        X, Y = meshgrid_of(Z)
    elif X is None:
        X, _ = meshgrid_of(Z)
    elif Y is None:
        _, Y = meshgrid_of(Z)

    if C is None:
        C = Z

    scalarMap = cm.ScalarMappable(norm=Normalize(vmin=C.min(), vmax=C.max()), cmap=colormap)

    # outputs an array where each C value is replaced with a corresponding color value
    C_colored = scalarMap.to_rgba(C)

    ax = gca(projection='3d')

    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=C_colored, shade=shade)

    return surf


def meshgrid_of(A):
    xx, yy = meshgrid(range(shape(A)[1]), range(shape(A)[0]))
    return xx, yy