我正在尝试使用 sns 绘制 python 中粒子角方向的一些数据的 PDF。数据覆盖 -180,180 度范围,我在分布拟合方面遇到问题,尤其是边缘周围
这是我用于绘图的代码片段
for shear_value, color in zip(unique_shear_values, shear_palette_colors):
subset = orientations[orientations['Shear'] == shear_value]
sns.histplot(data=subset['Angle'], stat="density", kde=True, label=f'Shear: {shear_value}', color=color, bins = 128)
有没有办法获得更适合数据行为的 PDF,尤其是边缘数据?
一个技巧可能是复制角度小于 0 的所有数据加上 360°。类似地复制所有角度大于 0 的数据减去 360°。然后使用双箱创建直方图。同时将 x 轴限制在 -180 +180 范围内。
一个缺点是 y 轴将以一半高度显示所有内容。创建虚拟双轴可以显示正确的 y 缩放。
下面的代码说明了这个想法:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# create some reproducible test data
np.random.seed(20231011)
y, x = np.random.randn(2, 600, 3).cumsum(axis=1).reshape(2, -1)
angles = np.degrees(np.arctan2(y, x))
# double the range by repeating the angles to the left and to the right
angles = np.concatenate([angles, angles[angles < 0] + 360, angles[angles >= 0] - 360])
fig, ax = plt.subplots()
ax_twinx = ax.twinx()
sns.histplot(angles, kde=True, stat='density',
binrange=(-360, 360), bins=360, kde_kws={'bw_adjust': 0.5}, ax=ax_twinx)
ax.set_xlim(-180, 180)
ax.set_ylim(0, ax_twinx.get_ylim()[1] * 2) # set the limits to twice the limits of the dummy axis
ax_twinx.set_yticks([]) # remove the ticks of the dummy axis
ax.set_ylabel(ax_twinx.get_ylabel()) # copy the y-label
ax_twinx.set_ylabel(None) # remove the label of the dummy y-axis
sns.despine()
ax.set_title('repeating angles left and right')
plt.tight_layout()
plt.show()