You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

极坐标pcolormesh图Gouraud着色报错与无显示问题求助

极坐标pcolormesh切换Gouraud着色的问题

我用pcolormesh在极坐标图里绘制两个径向值之间的环形直方图,用flat着色能正常运行,但切换成Gouraud着色时直接报错:

Dimensions of C (1, 10) are incompatible with X (11) and/or Y (2); see help(pcolormesh)

如果改成下面的代码:

pcm = ax.pcolormesh(theta_mesh[:-1,:-1]*np.pi/180, r_mesh[:-1,:-1], hist, cmap=cmap, shading='gouraud')

虽然不报错,但图里完全没有内容显示。完整示例代码如下:

import numpy as np
import matplotlib.pyplot as plt
from numpy.ma import masked_array

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}, figsize = (6,6))
ax.set_theta_zero_location('S')
ax.set_theta_direction(1)
theta_ticks = np.array([10, 0, -10, -20, -30, -40])
theta_labels = [f'{t}' for t in theta_ticks]
ax.set_xticks(np.radians(theta_ticks))
ax.set_xticklabels(theta_labels)
r_ticks = np.linspace(1, 0.1, 10)
ax.set_yticks(r_ticks)
ax.set_rlim(1,0.1)
ax.set_axisbelow(True)
ax.grid(alpha=0.4)
ax.set_thetamin(-40)
ax.set_thetamax(10)

n_rings = 7
n_segments = 15

cmap = plt.get_cmap('rainbow')

r = np.array([0.27718767, 0.26992833, 0.3291085,  0.219035,   0.3291085,  0.32943633,
 0.30544525, 0.222413,   0.27754,    0.27278267, 0.286725,   0.219035,
 0.22321733, 0.30544525, 0.219035,   0.3291085,  0.271587,   0.328375,
 0.32943633, 0.30726033, 0.30726033, 0.33995833, 0.23273433, 0.27318767,
 0.27718767, 0.28464367, 0.3291085,  0.219035,   0.34695067, 0.271587,
 0.218609,   0.26349133, 0.309604,   0.20733267, 0.27786567, 0.309604,
 0.21290633, 0.27786567, 0.3291085,  0.271587,   0.3291085,  0.27754,
 0.324375,   0.23273433, 0.328375,   0.26992833, 0.32943633, 0.219035,
 0.27754,    0.322597,   0.27672,    0.339284  ])

th = np.array([ -6.19994195, -18.41777188,  -9.26144056, -12.03341037,  -8.558630,
  -8.95986186,  -7.35883334, -15.33784359, -21.03901875, -17.43569267,
  -7.51616751, -15.88593498, -14.20710742, -12.72758571, -13.4987869,
  -8.51990186, -14.85967987,  -7.99177069,  -9.02112836, -14.20955114,
  -7.66543414, -10.73250029, -14.20920819,  -5.27444818, -10.0430456,
  -7.29558638, -13.72777957, -16.58168235,  -4.44479012, -14.77699067,
 -11.55023034, -14.37873904, -13.80156743, -17.03877898, -13.79688572,
  -8.34318779, -15.93602449, -14.96785248,  -4.20489867,  -6.34260421,
  -8.6836805,  -12.40561434, -11.65899316, -19.48056034, -13.24884988,
 -12.03800774,  -9.85039603,  -7.51309429,  -9.20345638, -15.12545221,
 -11.15248177, -11.70093429]) # degrees

hist, r_edges, theta_edges = np.histogram2d(r, th, range=[r_range, theta_range], bins=[n_rings, n_segments])

r_range = [0.2, 0.3]
theta_range = [-25, 0]
idx = np.where((r <= 0.3) & (r > 0.2))[0]
hist, r_edges, theta_edges = np.histogram2d(r[idx], th[idx], range=[r_range, theta_range], bins=[1, 10])
hist = masked_array(hist, hist == 0) # remove empty bins
theta_mesh, r_mesh = np.meshgrid(theta_edges, r_edges)
pcm = ax.pcolormesh(theta_mesh*np.pi/180, r_mesh, hist, cmap=cmap, shading='flat')

问题原因

Gouraud着色要求C数组的维度和X/Y网格的维度完全一致,因为它需要为每个网格顶点提供颜色值来进行插值:

  • flat着色时,C的维度是(n_rings, n_segments),对应X/Y网格去掉最后一行/列后的维度,因此能正常工作;
  • 当你截取theta_mesh[:-1,:-1]和r_mesh[:-1,:-1]时,网格维度变为(1,10),虽然和hist维度匹配,但极坐标下的环形区域径向范围极小,加上Gouraud插值依赖顶点颜色,而现有数据只有单元格中心的颜色值,最终无法渲染出可见内容。

解决方法

要适配Gouraud着色,需要扩展hist数组,使其维度与X/Y网格完全一致,为每个顶点补充颜色值:

# 扩展hist数组,使其维度与theta_mesh、r_mesh一致
hist_gouraud = np.zeros_like(theta_mesh)
# 填充单元格对应的颜色值
hist_gouraud[:-1, :-1] = hist
# 复制最后一行和列的颜色值,为环形内外径的顶点补充颜色
hist_gouraud[-1, :] = hist_gouraud[-2, :]
hist_gouraud[:, -1] = hist_gouraud[:, -2]

# 使用Gouraud着色绘制
pcm = ax.pcolormesh(theta_mesh*np.pi/180, r_mesh, hist_gouraud, cmap=cmap, shading='gouraud')
plt.colorbar(pcm)
plt.show()

另外注意原代码中r_range和theta_range的定义顺序错误,需要先定义范围再调用histogram2d。调整后的完整可运行代码:

import numpy as np
import matplotlib.pyplot as plt
from numpy.ma import masked_array

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}, figsize = (6,6))
ax.set_theta_zero_location('S')
ax.set_theta_direction(1)
theta_ticks = np.array([10, 0, -10, -20, -30, -40])
theta_labels = [f'{t}' for t in theta_ticks]
ax.set_xticks(np.radians(theta_ticks))
ax.set_xticklabels(theta_labels)
r_ticks = np.linspace(1, 0.1, 10)
ax.set_yticks(r_ticks)
ax.set_rlim(1,0.1)
ax.set_axisbelow(True)
ax.grid(alpha=0.4)
ax.set_thetamin(-40)
ax.set_thetamax(10)

n_rings = 7
n_segments = 15

cmap = plt.get_cmap('rainbow')

r = np.array([0.27718767, 0.26992833, 0.3291085,  0.219035,   0.3291085,  0.32943633,
 0.30544525, 0.222413,   0.27754,    0.27278267, 0.286725,   0.219035,
 0.22321733, 0.30544525, 0.219035,   0.3291085,  0.271587,   0.328375,
 0.32943633, 0.30726033, 0.30726033, 0.33995833, 0.23273433, 0.27318767,
 0.27718767, 0.28464367, 0.3291085,  0.219035,   0.34695067, 0.271587,
 0.218609,   0.26349133, 0.309604,   0.20733267, 0.27786567, 0.309604,
 0.21290633, 0.27786567, 0.3291085,  0.271587,   0.3291085,  0.27754,
 0.324375,   0.23273433, 0.328375,   0.26992833, 0.32943633, 0.219035,
 0.27754,    0.322597,   0.27672,    0.339284  ])

th = np.array([ -6.19994195, -18.41777188,  -9.26144056, -12.03341037,  -8.558630,
  -8.95986186,  -7.35883334, -15.33784359, -21.03901875, -17.43569267,
  -7.51616751, -15.88593498, -14.20710742, -12.72758571, -13.4987869,
  -8.51990186, -14.85967987,  -7.99177069,  -9.02112836, -14.20955114,
  -7.66543414, -10.73250029, -14.20920819,  -5.27444818, -10.0430456,
  -7.29558638, -13.72777957, -16.58168235,  -4.44479012, -14.77699067,
 -11.55023034, -14.37873904, -13.80156743, -17.03877898, -13.79688572,
  -8.34318779, -15.93602449, -14.96785248,  -4.20489867,  -6.34260421,
  -8.6836805,  -12.40561434, -11.65899316, -19.48056034, -13.24884988,
 -12.03800774,  -9.85039603,  -7.51309429,  -9.20345638, -15.12545221,
 -11.15248177, -11.70093429]) # degrees

# 先定义范围再调用histogram2d
r_range = [0.2, 0.3]
theta_range = [-25, 0]
idx = np.where((r <= 0.3) & (r > 0.2))[0]
hist, r_edges, theta_edges = np.histogram2d(r[idx], th[idx], range=[r_range, theta_range], bins=[1, 10])
hist = masked_array(hist, hist == 0) # remove empty bins
theta_mesh, r_mesh = np.meshgrid(theta_edges, r_edges)

# 扩展hist数组适配Gouraud着色
hist_gouraud = np.zeros_like(theta_mesh)
hist_gouraud[:-1, :-1] = hist
# 复制最后一行和列的颜色值
hist_gouraud[-1, :
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 23:55:38