Python极坐标等高线图绘制求助:非均匀数据插值与报错问题
极坐标等高线图绘制问题及解决
问题背景
现有20000组非均匀分布的XYZ实测数据(含极坐标角度分量),绘制极坐标等高线图时遇到以下问题:
- 尝试
pet.contour失败 - 使用
tricontourf得到的图效果差,需要更好的插值效果 - 使用
scipy.interpolate.griddata时触发TypeError:griddata() got multiple values for argument 'method',调整contourf/contour等方法仍报错,怀疑数组格式问题但不知如何修正
尝试的tricontourf代码
# 注:原代码导入pandas别名错误,此处修正为pd import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.read_excel('......xlsx', ...) data_arr = df.to_numpy() direction = np.radians(data_arr[:,1]) values = data_arr[:,0] zeniths = data_arr[:,2] r, theta = np.meshgrid(zeniths, direction) fig, ax = plt.subplots(subplot_kw=dict(projection='polar')) # 颜色刻度范围 lmin = min(data_arr[:,0]) lmax = max(data_arr[:,0]) colormap = plt.get_cmap('rainbow') norm = plt.colors.Normalize(lmin, lmax) ax.set_theta_zero_location("N") ax.set_theta_direction(-1) ax.tricontourf(direction, zeniths, values, 30, cmap=colormap) plt.show()
报错的griddata代码及错误信息
代码
# 注:原代码导入pandas别名错误,此处修正为pd import pandas as pd import numpy as np import matplotlib.pyplot as plt from scipy import interpolate df = pd.read_excel('...xlsx') data_arr = df.to_numpy() direction = np.radians(data_arr[:,1]) values = data_arr[:,0] zeniths = data_arr[:,2] def grid(direction, zeniths, values, resX=100, resY=100): xi = np.linspace(min(direction), max(direction), resX) yi = np.linspace(min(zeniths), max(zeniths), resY) Z = interpolate.griddata(direction, zeniths, values, xi, yi, method='cubic') X, Y = np.meshgrid(xi, yi) return X, Y, Z X, Y, Z = grid(direction, zeniths, values) contour = plt.contourf(X,Y,Z)
错误信息
Exception has occurred: TypeError griddata() got multiple values for argument 'method' File “…Contour polarPlot 2", line 20, in Z = interpolate.griddata(direction, zeniths, values, xi, yi, method='cubic') File "/ Contour polarPlot 2", line 23, in <module> X, Y, Z = grid(direction, zeniths, values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: griddata() got multiple values for argument 'method'
问题解决
1. griddata参数错误修正
scipy.interpolate.griddata的正确参数格式要求:
- 第一个参数是**(N,2)的二维数组**,组合所有原始点的坐标
- 插值网格需以
(Xi, Yi)形式传入(meshgrid生成的网格对)
修正后的grid函数:
def grid(direction, zeniths, values, resX=100, resY=100): # 组合原始点坐标为(N,2)数组 points = np.column_stack((direction, zeniths)) # 生成插值网格 xi = np.linspace(direction.min(), direction.max(), resX) yi = np.linspace(zeniths.min(), zeniths.max(), resY) Xi, Yi = np.meshgrid(xi, yi) # 执行插值 Z = interpolate.griddata(points, values, (Xi, Yi), method='cubic') return Xi, Yi, Z
2. 极坐标等高线图绘制优化
修正插值后,结合极坐标投影绘制高质量等高线图:
# 调用修正后的grid函数 theta_grid, r_grid, Z_grid = grid(direction, zeniths, values) fig, ax = plt.subplots(subplot_kw=dict(projection='polar')) # 设置极坐标方向(北为0度,顺时针旋转) ax.set_theta_zero_location("N") ax.set_theta_direction(-1) # 绘制等高线图 contour = ax.contourf(theta_grid, r_grid, Z_grid, 30, cmap='rainbow') # 添加颜色条 plt.colorbar(contour) plt.show()
3. 其他注意事项
- 导入pandas时不要用
df作为别名,避免覆盖后续的DataFrame变量 - 若
cubic插值效果仍不理想,可尝试linear或nearest方法,或提高插值分辨率(resX/resY) - 确保角度已转换为弧度(原代码已处理,无需修改)
内容的提问来源于stack exchange,提问作者Slawo Lomnicki
相关产品推荐
相关产品推荐

