Python 中给定 r、theta 和 z 值的极坐标直方图

Polar histogram in Python for given r, theta and z values

我有一个数据框,其中包含特定磁力计站随时间的测量值,列对应于:

我想知道如何将此数据帧绘制为测量变量的极坐标直方图:即像这样的东西:

我看过 physt 中的特殊直方图,但这让我只能输入 x,y 值,我对此感到很困惑。

有人能帮忙吗?

这似乎是您要查找的内容:https://physt.readthedocs.io/en/latest/special_histograms.html#Polar-histogram

from physt import histogram, binnings, special
import numpy as np
import matplotlib.pyplot as plt

# Generate some points in the Cartesian coordinates
np.random.seed(42)

x = np.random.rand(1000)
y = np.random.rand(1000)
z = np.random.rand(1000)

# Create a polar histogram with default parameters
hist = special.polar_histogram(x, y)
ax = hist.plot.polar_map()

链接的文档包含更多关于颜色、bin 大小等的示例。

编辑: 我认为这需要一些修改才能使您的数据具有正确的形状,但我认为这个示例说明了库的功能并且可以进行调整对于您的用例:

import random
import numpy as np
import matplotlib.pyplot as plt
from physt import special

# Generate some points in the Cartesian coordinates
np.random.seed(42)

gen = lambda l, h, s = 3000: np.asarray([random.random() * (h - l) + l for _ in range(s)])

X = gen(-100, 100)
Y = gen(-1000, 1000)
Z = gen(0, 1400)

hist = special.polar_histogram(X, Y, weights=Z, radial_bins=40)
# ax = hist.plot.polar_map()

hist.plot.polar_map(density=True, show_zero=False, cmap="inferno", lw=0.5, figsize=(5, 5))
plt.show()

使用 numpy.histogram2d. Plotting the resulting 2D array can be done with matplotlib's pcolormesh 即可轻松计算直方图。

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt

# two input arrays
azimut = np.random.rand(3000)*2*np.pi
radius = np.random.rayleigh(29, size=3000)

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

#calculate 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="magma_r")
fig.colorbar(pc)

plt.show()