Seaborn / Matplotlib:在不扭曲 bin 大小的情况下修改双变量直方图的轴限制
Seaborn / Matplotlib: Modify axis limits of bivariate histogram without distorting bin size
我正在尝试绘制两个具有相同轴限制的双变量直方图。当我只是绘制它们而不试图改变它们的任何一个轴时,我得到了这个(省略了一些样板参数数据定义和 x/y 刻度格式):
def create_hist_plot(params, ax, cbar_ax=None, color=GK_GREEN, bins=80):
result = sns.histplot(
**params,
ax=ax,
kde=True,
bins=bins,
color=color,
)
return result
ax_2 = create_hist_plot(params_1, ax_2, color=GREEN, bins=40)
ax_3 = create_hist_plot(params_2, ax_3, color=PURPLE, bins=40)
我想调整紫色直方图的大小,使其与绿色直方图具有相同的轴范围。我试过像这样手动设置限制(ax_3 是紫色直方图,ax_2 是绿色直方图)
ax_3.set_ylim(ax_2.get_ylim())
ax_3.set_xlim(ax_2.get_xlim())
这给了我
但请注意紫色直方图中的 bin 现在是如何扭曲的。有没有办法在不扭曲 bin 的情况下修改直方图的轴限制?
根据johanc在评论中的推荐,我尝试了以下方法,得到了我想要的结果。
def create_hist_plot(params, ax, cbar_ax=None, color=GK_GREEN, bins=80):
result = sns.histplot(
**params,
ax=ax,
kde=True,
bins=bins,
color=color,
)
return result
ax_2 = create_hist_plot(params_1, ax_2, color=GREEN, bins=40)
bins = [
np.linspace(ax_2.get_xlim()[0], ax_2.get_xlim()[1], 40),
np.linspace(ax_2.get_ylim()[0], ax_2.get_ylim()[1], 40)
]
ax_3 = create_hist_plot(params_2, ax_3, color=PURPLE, bins=bins)
结果
我正在尝试绘制两个具有相同轴限制的双变量直方图。当我只是绘制它们而不试图改变它们的任何一个轴时,我得到了这个(省略了一些样板参数数据定义和 x/y 刻度格式):
def create_hist_plot(params, ax, cbar_ax=None, color=GK_GREEN, bins=80):
result = sns.histplot(
**params,
ax=ax,
kde=True,
bins=bins,
color=color,
)
return result
ax_2 = create_hist_plot(params_1, ax_2, color=GREEN, bins=40)
ax_3 = create_hist_plot(params_2, ax_3, color=PURPLE, bins=40)
我想调整紫色直方图的大小,使其与绿色直方图具有相同的轴范围。我试过像这样手动设置限制(ax_3 是紫色直方图,ax_2 是绿色直方图)
ax_3.set_ylim(ax_2.get_ylim())
ax_3.set_xlim(ax_2.get_xlim())
这给了我
但请注意紫色直方图中的 bin 现在是如何扭曲的。有没有办法在不扭曲 bin 的情况下修改直方图的轴限制?
根据johanc在评论中的推荐,我尝试了以下方法,得到了我想要的结果。
def create_hist_plot(params, ax, cbar_ax=None, color=GK_GREEN, bins=80):
result = sns.histplot(
**params,
ax=ax,
kde=True,
bins=bins,
color=color,
)
return result
ax_2 = create_hist_plot(params_1, ax_2, color=GREEN, bins=40)
bins = [
np.linspace(ax_2.get_xlim()[0], ax_2.get_xlim()[1], 40),
np.linspace(ax_2.get_ylim()[0], ax_2.get_ylim()[1], 40)
]
ax_3 = create_hist_plot(params_2, ax_3, color=PURPLE, bins=bins)
结果