Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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 如何从matplotlib中的x、y、z坐标绘制等高线图?(plt.contourf或plt.contourf)_Python_Numpy_Matplotlib_Contour_Topology - Fatal编程技术网

Python 如何从matplotlib中的x、y、z坐标绘制等高线图?(plt.contourf或plt.contourf)

Python 如何从matplotlib中的x、y、z坐标绘制等高线图?(plt.contourf或plt.contourf),python,numpy,matplotlib,contour,topology,Python,Numpy,Matplotlib,Contour,Topology,这些meshgrid对我来说有点让人困惑。我试图用x和y坐标绘制散点图,并在散点图上覆盖一个等高线图,连续排列z坐标。类似于立面图 如果我对x、y和z坐标使用meshgrid,那么我会得到每个坐标的3D阵列,这仍然是不正确的输入 df_xyz = pd.read_table("https://pastebin.com/raw/f87krHFK", sep="\t", index_col=0) x = df_xyz.iloc[:,0].values y = df_xyz.iloc[:,1].val

这些
meshgrid
对我来说有点让人困惑。我试图用
x
y
坐标绘制散点图,并在散点图上覆盖一个等高线图,连续排列
z
坐标。类似于立面图

如果我对x、y和z坐标使用
meshgrid
,那么我会得到每个坐标的3D阵列,这仍然是不正确的输入

df_xyz = pd.read_table("https://pastebin.com/raw/f87krHFK", sep="\t", index_col=0)
x = df_xyz.iloc[:,0].values
y = df_xyz.iloc[:,1].values
z = df_xyz.iloc[:,2].values

XX, YY = np.meshgrid(x,y)
with plt.style.context("seaborn-white"):
    fig, ax = plt.subplots(figsize=(13,8))
    ax.scatter(x,y, color="black", linewidth=1, edgecolor="ivory", s=50)
    ax.contourf(XX,YY,z)
#     TypeError: Input z must be a 2D array.

XX, YY, ZZ = np.meshgrid(x,y,z)
with plt.style.context("seaborn-white"):
    fig, ax = plt.subplots(figsize=(13,8))
    ax.scatter(x,y, color="black", linewidth=1, edgecolor="ivory", s=50)
    ax.contourf(XX,YY,ZZ)
#     TypeError: Input z must be a 2D array.
这是我当前的输出:

我正在尝试做类似的事情:

您只需使用
tricontourf
然后在顶部分散即可
import pandas as pd
import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
%matplotlib inline

df_xyz = pd.read_table("https://pastebin.com/raw/f87krHFK", sep="\t", index_col=0)
x = df_xyz.iloc[:,0].values
y = df_xyz.iloc[:,1].values
z = df_xyz.iloc[:,2].values

def plot_contour(x,y,z,resolution = 50,contour_method='linear'):
    resolution = str(resolution)+'j'
    X,Y = np.mgrid[min(x):max(x):complex(resolution),   min(y):max(y):complex(resolution)]
    points = [[a,b] for a,b in zip(x,y)]
    Z = griddata(points, z, (X, Y), method=contour_method)
    return X,Y,Z

X,Y,Z = plot_contour(x,y,z,resolution = 50,contour_method='linear')

with plt.style.context("seaborn-white"):
    fig, ax = plt.subplots(figsize=(13,8))
    ax.scatter(x,y, color="black", linewidth=1, edgecolor="ivory", s=50)
    ax.contourf(X,Y,Z)