如何在 matplotlib 极坐标 2D 直方图中创建弯曲的 bin?

问题描述

我正在使用 matplotlib 和以下代码(改编自 this answer to another question)在 Python 3.7 中绘制极坐标 2d 直方图:

import numpy as np
import matplotlib.pyplot as plt

# input data
azimut = np.random.rand(3000)*2*np.pi
radius = np.random.rayleigh(9,size=3000)

# binning
rbins = np.linspace(0,radius.max(),10)
abins = np.linspace(0,2*np.pi,10)

# histogram
hist,_,_ = np.histogram2d(azimut,radius,bins=(abins,rbins))
A,R = np.meshgrid(abins,rbins)

# plot
fig,ax = plt.subplots(subplot_kw=dict(projection="polar"))

pc = ax.pcolormesh(A,R,hist.T,cmap='inferno')
fig.colorbar(pc)

plt.show()

生成以下图:

enter image description here

由于 bin 尺寸较大,极坐标投影看起来更像多边形而不是圆形。

有什么办法可以绘制这个图,使垃圾箱看起来是弯曲的而不是直的? IE。这样无论 bin 大小如何,绘图始终是圆形的,并且当 bin 较大时不会变成多边形?

最好使用 matplotlib 解决方案,但也欢迎其他解决方案。

非常感谢您的帮助。

解决方法

要获得圆形外观,可以将网格细分为更多角度。请注意,header_image() 创建了 9 个 bin(和 10 个边界)。对于细分网格,您需要例如90 个 bin,所以有 91 个边界。直方图值需要重复相同的因子。

以下代码使用不同的颜色图进行调试。可选网格突出显示原始边界。

echo

rounded pcolormesh on polar axes