Logo
Section 1 / 8

使用条形图进行可视化

学习如何绘制、解读和美化条形图以进行数据分析。

导入

使用条形图进行可视化

学习如何绘制、解读和美化条形图以进行数据分析。

条形图,也称为分类估计图,是通过自助法(bootstrapping)技术计算每个类别中唯一组别的均值和置信区间。计数图和条形图有许多相似之处:计数图统计每个类别的实例数量,而条形图则计算每组的均值及其置信区间。

我们首先导入所需的库和存储在 titanic_df DataFrame 中的 titanic 数据集。在条形图中,通常包含一个分类变量和一个数值变量。例如,使用 sns.barplot() 函数,我们可以根据 pclass 对乘客进行分类,并可视化每个 pclass 类别下 age 的分布情况。此外,图表中的条形带有垂直线,这些垂直线是误差棒,用作数据变异性的视觉指示,表示报告测量值中的误差或不确定性。

此外,我们可以使用 bar_label() 函数自定义条形标签。由于条形图显示的是均值(浮点数),可以通过设置 fmt= "%.2f" 来格式化浮点数,使其保留小数点后两位。我们还可以使用 ylim() 函数增加 y 轴范围,以确保标签和条形完全显示在图形内。

1c_plot = sns.barplot(data=titanic_df, x='pclass', y='age') # 按 pclass 分类 2plt.bar_label(c_plot.containers[0], padding=13, color='red', 3 fontsize=14, fmt= "%.2f", label_type='edge') 4plt.ylim(top=50) 5plt.savefig('output/graph.png')

上述条形图的效果等同于在下方代码框中对 DataFrame 使用 pandas 函数。我们使用 groupby() 函数按传入的参数值对 DataFrame 进行分组,然后访问 age 列并调用 mean() 函数。我们可以看到每个 pclass 的计算均值,这与条形图中的结果一致。

1print(titanic_df.groupby('pclass').age.mean())

我们可以通过 order 参数自定义垂直条形的显示顺序。在 sns.barplot() 函数中指定 order=[3,2,1]。此外,我们可以在 bar_label() 函数中设置 label_type='center',使标签居中显示在条形上。

1c_plot = sns.barplot(data=titanic_df, x='pclass', y='age', order=[3,2,1]) 2plt.bar_label(c_plot.containers[0], color='black', 3 fontsize=15, fmt= "%.2f", label_type='center') 4plt.savefig('output/graph.png')

默认情况下,seaborn 使用 95% 的置信区间,但可以根据需要进行更改。如下所示,通过在 sns.barplot() 函数中设置 ci=50,将置信区间更改为 50%。我们也可以设置 ci=None,此时条形图将不显示置信区间。

1c_plot = sns.barplot(data=titanic_df, x='pclass', y='age', ci=50) 2plt.bar_label(c_plot.containers[0], padding=4, color='black', 3 fontsize=15, fmt= "%.2f", label_type='center') 4plt.savefig('output/graph.png')

我们可以修改条形图中使用的自助样本数量。默认为 1000 次,但可以通过在 sns.barplot() 函数中设置 n_boot=500 将其更改为 500 次,如下所示:

1c_plot = sns.barplot(data=titanic_df, x='pclass', y='age', ci=95, n_boot=500) 2plt.bar_label(c_plot.containers[0], padding=12, color='red', 3 fontsize=15, fmt= "%.2f", label_type='edge') 4plt.ylim(top=50) 5plt.savefig('output/graph.png')

我们可以使用 hue 参数对条形图进行颜色编码。在 sns.barplot() 函数中指定 hue='who'。在 bar_label() 函数中为每组使用不同的条形颜色。该图展示了每个乘客舱位等级中“男性”、“女性”和“儿童”类别的比例。

1c_plot = sns.barplot(data=titanic_df, x='pclass', y='age', hue='who') # 添加 hue 2# 第一组 hue 3plt.bar_label(c_plot.containers[0], padding=18, color='red', 4 fontsize=10, fmt= "%.2f", label_type='edge') 5# 第二组 hue 6plt.bar_label(c_plot.containers[1], padding=18, color='black', 7 fontsize=10, fmt= "%.2f", label_type='edge') 8# 第三组 hue 9plt.bar_label(c_plot.containers[2], padding=18, color='green', 10 fontsize=10, fmt= "%.2f", label_type='edge') 11plt.ylim(top=60) 12plt.savefig('output/graph.png')

我们还可以进一步调整 hue 类别的显示顺序。通过设置 hue_order=['child','woman','man'],这一顺序将在生成的条形图中体现出来:

1c_plot = sns.barplot(data=titanic_df, x='pclass', y='age', 2 hue='who', hue_order=['child','woman','man']) 3# 第一组 hue 4plt.bar_label(c_plot.containers[0], padding=18, color='red', 5 fontsize=10, fmt= "%.2f", label_type='edge') 6# 第二组 hue 7plt.bar_label(c_plot.containers[1], padding=18, color='black', 8 fontsize=10, fmt= "%.2f", label_type='edge') 9# 第三组 hue 10plt.bar_label(c_plot.containers[2], padding=18, color='green', 11 fontsize=10, fmt= "%.2f", label_type='edge') 12plt.ylim(top=60) 13plt.savefig('output/graph.png')

要将条形图的方向从垂直改为水平,只需将分类变量放在 y 参数,数值变量放在 x 参数,并在 sns.barplot() 函数中使用 orient='h' 属性。我们把标签显示在条形中心,因此无需调整坐标轴范围。

1c_plot = sns.barplot(data=titanic_df, x='age', y='pclass', orient='h') 2plt.bar_label(c_plot.containers[0], color='black', 3 fontsize=15, fmt= "%.2f", label_type='center') 4plt.savefig('output/graph.png')

我们可以自定义垂直条形上的误差棒。要更改其颜色,设置 errcolor='red';要更改其宽度,设置 errwidth=5.0。带有自定义误差棒的条形图如下所示:

1c_plot = sns.barplot(data=titanic_df, x='pclass', y='age', 2 hue='who', errcolor='red', errwidth=5.0) 3# 第一组 hue 4plt.bar_label(c_plot.containers[0], padding=13, color='black', 5 fontsize=10, fmt= "%.2f", label_type='edge') 6# 第二组 hue 7plt.bar_label(c_plot.containers[1], padding=13, color='black', 8 fontsize=10, fmt= "%.2f", label_type='edge') 9# 第三组 hue 10plt.bar_label(c_plot.containers[2], padding=13, color='black', 11 fontsize=10, fmt= "%.2f", label_type='edge') 12plt.ylim(top=60) 13plt.savefig('output/graph.png')

通过在 sns.barplot() 函数中设置 capsize=0.2,可以自定义误差棒的大小。同时,我们使用 bar_label() 函数对条形标签进行样式设置。

1c_plot = sns.barplot(data=titanic_df, x='pclass', y='age', 2 capsize=0.2, errcolor='red', errwidth=5.0) 3plt.bar_label(c_plot.containers[0], color='black', 4 fontsize=15, fmt= "%.2f", label_type='center') 5plt.ylim(top=50) 6plt.savefig('output/graph.png')

绘制数值数据

使用点图进行可视化