如何从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)

查看:478
本文介绍了如何从matplotlib中的x,y,z坐标绘制等高线图? (plt.contourf或plt.contour)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些meshgrid对我来说有点令人困惑.我正在尝试使用xy坐标绘制散点图,并在散点图上覆盖轮廓图,并连续散布z坐标.类似于立面图.

These meshgrid is a little confusing to use for me. I'm trying to do a scatter plot with the x and y coordinates with a contour plot overlaid on the scatter with a continuous spread for the z coordinates. Similar to an elevation map.

如果我将xc,y和z坐标与meshgrid一起使用,则我得到的3D数组仍然是错误的输入.

If I use meshgrid with the x,y, and z coordinates then I get 3D array for each which is still the incorrect input.

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.

这是我当前的输出:

Here's my current output:

我正在尝试执行以下操作:

I am trying to do something similar to this:

推荐答案

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)

这篇关于如何从matplotlib中的x,y,z坐标绘制等高线图? (plt.contourf或plt.contour)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆