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

前端 未结 1 1446
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 01:58

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

相关标签:
1条回答
  • 2020-12-20 02:55
    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)
    
    0 讨论(0)
提交回复
热议问题