繁体   English   中英

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

[英]How to do a contour plot from x,y,z coordinates in matplotlib? (plt.contourf or plt.contour)

这些meshgrid对我来说有点令人困惑。 我正在尝试使用xy坐标绘制散点图,并在散点图上叠加轮廓图,并为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.

这是我当前的输出: 在此处输入图片说明

我正在尝试做类似的事情: 在此处输入图片说明

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM