【问题标题】:Contour Matplotlib from scatter data - python来自散点数据的轮廓 Matplotlib - python
【发布时间】:2022-06-14 20:21:04
【问题描述】:

我正在查看一些磁盘厚度的数据,并尝试使用 matplotlib 轮廓绘制它。

我所拥有的让我能够制作出这样的东西:

我想要的更像是:

我所能找到的与轮廓相关的只是 Z 是一个方程,而不是散点。

也许我需要一个补全/插值函数来补全缺失的部分。

以下是一些用于测试的数据,我无法从 plt.contourf 的正确设置中得出一些结果

把它放在一个圈子里很好,但不是强制性的。

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

a = pd.DataFrame({'X': [-9, -5, -5, -5, -3, -3, -3, 0, 0, 0, 3, 3, 3, 5, 5, 5, 9],
                      'Y':[0, -5, 0, 5, -3, 0, 3, -9, 0, 9, -3, 0, 3, -5, 0, 5, 0],
                      'Z':[5, 5.1, 5, 5, 5.4, 5.1, 5.3, 5.9, 5, 5, 5.3, 5.1, 5, 5.2, 5.3, 5.4, 5.]})
    
##%              
a_pivoted = a.pivot_table(index='Y', columns='X', values='Z')
ax = sns.heatmap(a_pivoted, annot=True)

##%
X, Y = np.meshgrid(np.unique(a['X']), np.unique(a['Y']))
Z = a.pivot_table(index='Y', columns='X', values='Z').values

fig,ax=plt.subplots(1,1)
cp = ax.contourf(X, Y, Z)
ax.set_title('Filled Contours Plot')
ax.set_xlabel('x (cm)')
ax.set_ylabel('y (cm)')
plt.show()

【问题讨论】:

    标签: python matplotlib surface


    【解决方案1】:

    几乎明白了:

    def ContourPlot(x, y, z, how_many_levels=17, title_plot='-', only_save=False): #read data from excel
    if np.max(x) < 10:
        radius_wafer_edge = 10.
    else:
        radius_wafer_edge = 100000.
    # Create grid values first.
    npts = 1000
    xi = np.linspace(-radius_wafer_edge, radius_wafer_edge, npts)
    yi = np.linspace(-radius_wafer_edge, radius_wafer_edge, npts)
    # here it can be optimized still...
    zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='linear')
    # zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='nearest')
    # zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='cubic')
    
    fig,ax=plt.subplots(1,1)
    cp = ax.contourf(xi, yi, zi, levels=how_many_levels, cmap="RdBu_r")
    fig.colorbar(cp) # Add a colorbar to a plot
    ax.set_title(title_plot)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set(xlim=(-radius_wafer_edge, radius_wafer_edge), ylim=(-radius_wafer_edge, radius_wafer_edge))
    draw_circle = plt.Circle((0, 0), radius_wafer_edge, fill=False)
    ax.add_artist(draw_circle)
    ax.set_aspect('equal', 'box')
    if only_save:
        plt.savefig(f'{title_plot}.png')
    else:
        plt.show()
    

    第一个 if 将您带到我之前共享创建的测试数据之间,并且您可以获得真实数据: https://imgur.com/pQIVTkx

    在晶圆边缘进行插值(实际上是外插)仍有一些改进,但对于我的目的来说已经足够了。

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 2015-05-07
      相关资源
      最近更新 更多