【问题标题】:Constructing a Contour Plot with Three Different Arrays Corresponding to X, Y, and Z in Python在 Python 中用对应于 X、Y 和 Z 的三个不同数组构造等高线图
【发布时间】:2016-12-12 01:44:21
【问题描述】:

如果我有对应于由数组分隔的 z 值的特定 x 和 y 值,我将如何制作等高线图?例如:

Array 1 (X):
1
4
6
7
8
2
6

Array 2 (Y):
7
7
8
9
0
1
2

Array 3 (Z):
8
9 
7
1
2
2
3

我是否必须做 X1, Y1 = np.meshgrid(X, Y) 并以某种方式塑造 Z 数组?在不使用网格网格的情况下是否有另一种方法可以做到这一点?另外,如果我添加第四个数组并将其命名为 Z1,并使用与特定 Z1 对应的相同 x 和 y 值,我可以将此等值线图与第一个等值线图一起绘制吗?

【问题讨论】:

    标签: python arrays matplotlib grid contour


    【解决方案1】:

    如果您没有规则的网格,使用三角曲面插值可能是一个不错的选择。

    在这个例子和上面的例子中,如果你有更长的数据,你只需要检查绘图的边界。

    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    import matplotlib.tri as tri
    
    sns.set(style="white")
    
    x = np.array([1,4,6,7,8,2,6])
    y = np.array([7,7,8,9,0,1,2])
    z = np.array([8,9,7,1,2,2,3])
    
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)
    
    nptsx, nptsy = 100, 100
    xg, yg = np.meshgrid(np.linspace(x.min(), x.max(), nptsx),
                         np.linspace(y.min(), y.max(), nptsy))
    
    triangles = tri.Triangulation(x, y)
    tri_interp = tri.CubicTriInterpolator(triangles, z)
    zg = tri_interp(xg, yg)
    
    # change levels here according to your data
    levels = np.linspace(0, 10, 5)
    colormap = ax.contourf(xg, yg, zg, levels,
                           cmap=plt.cm.Blues,
                           norm=plt.Normalize(vmax=z.max(), vmin=z.min()))
    
    # plot data points
    ax.plot(x, y, color="#444444", marker="o", linestyle="", markersize=10)
    
    # add a colorbar
    fig.colorbar(colormap,
                 orientation='vertical',  # horizontal colour bar
                 shrink=0.85)
    
    # graph extras: look at xlim and ylim
    ax.set_xlim((0, 10))
    ax.set_ylim((0, 10))
    ax.set_aspect("equal", "box")
    
    plt.show()
    

    这是输出:

    【讨论】:

      【解决方案2】:

      我认为你需要做一个插值:

      import numpy as np
      import matplotlib.pyplot as plt
      from mpl_toolkits.mplot3d import axes3d
      from scipy import interpolate
      x = np.array([1,4,6,7,8,2,6])
      y = np.array([7,7,8,9,0,1,2])
      z = np.array([8,9,7,1,2,2,3])
      points = np.column_stack((x,y))
      values = z.T
      
      gridx, gridy = np.mgrid[0:8:100j, 0:8:100j]
      gridz = interpolate.griddata(points, values, (gridx, gridy), method='cubic')
      
      fig = plt.figure(figsize=(12,5))
      
      ax1 = fig.add_subplot(121,projection='3d')
      ax1.plot3D(x,y,z, 'k.', ms=10)
      ax1.contour(gridx,gridy,gridz)
      
      ax2 = fig.add_subplot(122,projection='3d')
      ax2.plot3D(x,y,z, 'k.', ms=10)
      ax2.plot_wireframe(gridx, gridy, gridz,rstride=5,cstride=5)
      plt.savefig('contour_wire.png')
      

      这给出了:

      【讨论】:

      • 这很有帮助,但是假设我有一个大型数据集,每个 x、y 和 z 数组都有 411 个点。在这个程序中我需要改变什么?
      • 不,我只有三个数组,其中 (x(array 1),y(array 2)) 对应于特定的 z 值(数组 3)。
      猜你喜欢
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2013-02-26
      • 2020-12-05
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多