Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 给定一组3D点及其对应温度,如何绘制横截面的等高线图?_Python_Arrays_Algorithm_Matplotlib_Contour - Fatal编程技术网

Python 给定一组3D点及其对应温度,如何绘制横截面的等高线图?

Python 给定一组3D点及其对应温度,如何绘制横截面的等高线图?,python,arrays,algorithm,matplotlib,contour,Python,Arrays,Algorithm,Matplotlib,Contour,我有一个温度数组,还有一个3D点数组,这样温度[n]就是温度点[n]。如何绘制该数据的等高线图(查看二维平面) 我想创建一个函数extract\u plane,其中平面的参数方程将作为参数传递,然后返回其中的点和相应的温度(这就是我需要帮助的地方) 例如: import numpy as np import matplotlib.pyplot as plt points = np.array([[0., 0., 0.], [1., 0., 0.],

我有一个
温度数组
,还有一个3D
点数组
,这样
温度[n]
就是温度
点[n]
。如何绘制该数据的等高线图(查看二维平面)

我想创建一个函数
extract\u plane
,其中平面的参数
方程
将作为参数传递,然后返回其中的点和相应的温度(这就是我需要帮助的地方)

例如:

import numpy as np
import matplotlib.pyplot as plt

points = np.array([[0., 0., 0.],
                      [1., 0., 0.],
                      [1., 1., 0.],
                      [0., 1., 0.],
                      [0., 1., 1.],
                      [0., 0., 1.],
                      [1., 0., 1.],
                      [1., 1., 1.]])
temperature = np.array([0, 0, 0, 0, 1, 1, 1, 1.])
我需要帮助创建以下函数。实际上,它只提取平面中的点
z=0

def extract_plane(points, temperature, equation):
    """
    Given a set of 3D points, and their corresponding temperatures,
    extracts a set of points that are in the plane defined by equation
    along their temperatures.

    Parameters
    ----------
    points : ndarray (3D)
        The set of points.
    temperature : ndarray (1D)
        The temperatures at the points such that temperature[n] is 
        the temperature in points[n].
    equation : ????
        The equation that defines a 2D plane (cross-section) where
        the temperature is wanted to be plotted.

    Returns
    -------
    coord : ndarray (1D)
        The set of points that are in the plane defined by equation.
    temp : ndarray (1D)
        The set of temperatures in which temp[n] coresponds to coord[n].
    """
    temp = []
    coord = []
    # plane z=0
    for n in range(points.shape[0]):
        if (points[n,2] == 0.):
            temp += [temperature[n]]
            coord += [points[n]]

    temp = np.array(temp)
    coord = np.array(coord)
    return coord, temp
并使用中的
griddata
重塑
temp
,以便可以绘制:

# griddata.py - 2010-07-11 ccampo
def griddata(x, y, z, binsize=0.01, retbin=True, retloc=True):
    """
    Place unevenly spaced 2D data on a grid by 2D binning (nearest
    neighbor interpolation).

    Parameters
    ----------
    x : ndarray (1D)
        The idependent data x-axis of the grid.
    y : ndarray (1D)
        The idependent data y-axis of the grid.
    z : ndarray (1D)
        The dependent data in the form z = f(x,y).
    binsize : scalar, optional
        The full width and height of each bin on the grid.  If each
        bin is a cube, then this is the x and y dimension.  This is
        the step in both directions, x and y. Defaults to 0.01.
    retbin : boolean, optional
        Function returns `bins` variable (see below for description)
        if set to True.  Defaults to True.
    retloc : boolean, optional
        Function returns `wherebins` variable (see below for description)
        if set to True.  Defaults to True.

    Returns
    -------
    grid : ndarray (2D)
        The evenly gridded data.  The value of each cell is the median
        value of the contents of the bin.
    bins : ndarray (2D)
        A grid the same shape as `grid`, except the value of each cell
        is the number of points in that bin.  Returns only if
        `retbin` is set to True.
    wherebin : list (2D)
        A 2D list the same shape as `grid` and `bins` where each cell
        contains the indicies of `z` which contain the values stored
        in the particular bin.

    Revisions
    ---------
    2010-07-11  ccampo  Initial version
    """
    # get extrema values.
    xmin, xmax = x.min(), x.max()
    ymin, ymax = y.min(), y.max()

    # make coordinate arrays.
    xi      = np.arange(xmin, xmax+binsize, binsize)
    yi      = np.arange(ymin, ymax+binsize, binsize)
    xi, yi = np.meshgrid(xi,yi)

    # make the grid.
    grid           = np.zeros(xi.shape, dtype=x.dtype)
    nrow, ncol = grid.shape
    if retbin: bins = np.copy(grid)

    # create list in same shape as grid to store indices
    if retloc:
        wherebin = np.copy(grid)
        wherebin = wherebin.tolist()

    # fill in the grid.
    for row in range(nrow):
        for col in range(ncol):
            xc = xi[row, col]    # x coordinate.
            yc = yi[row, col]    # y coordinate.

            # find the position that xc and yc correspond to.
            posx = np.abs(x - xc)
            posy = np.abs(y - yc)
            ibin = np.logical_and(posx < binsize/2., posy < binsize/2.)
            ind  = np.where(ibin == True)[0]

            # fill the bin.
            bin = z[ibin]
            if retloc: wherebin[row][col] = ind
            if retbin: bins[row, col] = bin.size
            if bin.size != 0:
                binval         = np.median(bin)
                grid[row, col] = binval
            else:
                grid[row, col] = np.nan   # fill empty bins with nans.

    # return the grid
    if retbin:
        if retloc:
            return grid, bins, wherebin
        else:
            return grid, bins
    else:
        if retloc:
            return grid, wherebin
        else:
            return grid

这个问题的功能看起来比它需要的复杂得多。使用允许在栅格上插值

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

points = np.array([[0., 0., 0.],
                      [1., 0., 0.],
                      [1., 1., 0.],
                      [0., 1., 0.],
                      [0., 1., 1.],
                      [0., 0., 1.],
                      [1., 0., 1.],
                      [1., 1., 1.]])
temperature = np.array([0, 0, 0, 0, 1, 1, 1, 1.])

grid_y, grid_x = np.mgrid[0:1:25j, 0:1:25j]
# equation along which to interpolate
equation = lambda x,y : 0.8*(1-x)
grid_z = equation(grid_x, grid_y)

interp = griddata(points, temperature, (grid_x, grid_y, grid_z), method='linear')

plt.subplot(121)
#plt.contourf(grid_x,grid_y, interp,  origin='lower',vmin=0,vmax=1)
plt.imshow(interp,  origin='lower',vmin=0,vmax=1)
plt.title('temperature along 0.8*(1-x)')
plt.xlabel("x")
plt.ylabel("y")

from mpl_toolkits.mplot3d import Axes3D
ax = plt.subplot(122, projection=Axes3D.name)
ax.scatter(points[:,0], points[:,1], points[:,2], c=temperature)
ax.set_zlim(-.1,1.1)
ax.plot_surface(grid_x,grid_y,grid_z, facecolors=plt.cm.viridis(interp),
                linewidth=0, antialiased=False, shade=False)
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()

对于方程=λx,y:x*(y**.5):

当然,使用contourf也是同样可能的,
plt.contourf(grid_x,grid_y,interp,origin='lower',vmin=0,vmax=1)


问题中的函数看起来比需要的复杂得多。使用允许在栅格上插值

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt

points = np.array([[0., 0., 0.],
                      [1., 0., 0.],
                      [1., 1., 0.],
                      [0., 1., 0.],
                      [0., 1., 1.],
                      [0., 0., 1.],
                      [1., 0., 1.],
                      [1., 1., 1.]])
temperature = np.array([0, 0, 0, 0, 1, 1, 1, 1.])

grid_y, grid_x = np.mgrid[0:1:25j, 0:1:25j]
# equation along which to interpolate
equation = lambda x,y : 0.8*(1-x)
grid_z = equation(grid_x, grid_y)

interp = griddata(points, temperature, (grid_x, grid_y, grid_z), method='linear')

plt.subplot(121)
#plt.contourf(grid_x,grid_y, interp,  origin='lower',vmin=0,vmax=1)
plt.imshow(interp,  origin='lower',vmin=0,vmax=1)
plt.title('temperature along 0.8*(1-x)')
plt.xlabel("x")
plt.ylabel("y")

from mpl_toolkits.mplot3d import Axes3D
ax = plt.subplot(122, projection=Axes3D.name)
ax.scatter(points[:,0], points[:,1], points[:,2], c=temperature)
ax.set_zlim(-.1,1.1)
ax.plot_surface(grid_x,grid_y,grid_z, facecolors=plt.cm.viridis(interp),
                linewidth=0, antialiased=False, shade=False)
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()

对于方程=λx,y:x*(y**.5):

当然,使用contourf也是同样可能的,
plt.contourf(grid_x,grid_y,interp,origin='lower',vmin=0,vmax=1)


看一看。它将把你的数据,并把它放在一个二维网格就像你需要的。不过,你可能需要稍微修改一下输入。我认为你首先需要弄清楚你到底想展示什么。数据中有4个不同的点,每个点有两个不同的值。这将是三维立方体的边缘,但是如果你想在二维中绘制它,你不太清楚你想显示什么。看一看。它将把你的数据,并把它放在一个二维网格就像你需要的。不过,你可能需要稍微修改一下输入。我认为你首先需要弄清楚你到底想展示什么。数据中有4个不同的点,每个点有两个不同的值。这将是三维立方体的边,但是如果你想在二维中绘制它,你想显示什么还不是很清楚。我也在尝试做同样的事情,但是插值数据总是很难显示。我猜这是因为它试图在凸包外插值点,而我没有定义fill_值。但为什么他们都是南?我像这样定义网格,网格=np.mgrid[min\u y:max\u y:25j,min\u x:max\u x:25j]我尝试做同样的事情,但插值数据总是nan。我猜这是因为它试图在凸包外插值点,而我没有定义fill_值。但为什么他们都是南?我像这样定义了grid,grid=np.mgrid[min\u y:max\u y:25j,min\u x:max\u x:25j]