如何使用 Matplotlib 在 Seaborn 中在同一张图上绘制多个直方图?
matplotlibserver side programmingprogramming更新于 2025/7/3 22:22:17
要使用 Seaborn 在同一个图上绘制多个直方图,我们可以采取以下步骤 −
创建两个列表(x 和 y)。
创建一个图形并添加两个子图。
迭代由 x 和 y 组成的列表。
使用 histplot() 方法,使用列表中的数据绘制直方图(步骤 3)。
将 X 轴的范围限制在 0 到 10 之间。
要显示图形,请使用 show() 方法。
示例
import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 5, 1, 4, 2] y = [7, 5, 6, 4, 5] fig, ax = plt.subplots() for a in [x, y]: sns.histplot(a, bins=4, ax=ax, kde=False) ax.set_xlim([0, 10]) plt.show()
输出


