在特定位置画一条线/在 seaborn 中注释 Facetgrid [英] Draw a line at specific position/annotate a Facetgrid in seaborn

查看:47
本文介绍了在特定位置画一条线/在 seaborn 中注释 Facetgrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A 已经通过以下方式在 seaborn 中使用 Facetgrid 生成了箱线图

#导入数据集tips = sns.load_dataset(提示")# 使用 FacetGrid 绘制,以烟雾分隔plt.style.use('ggplot')g = sns.FacetGrid(tips, col=smoker", size=5, aspect=1.5)g.map(sns.boxplot, "sex", "total_bill", Palette='viridis', order=['Male', 'Female'])plt.show()

我现在想在每个图中绘制不同的水平线.例如,左侧图中的一条水平线(坐标 (0,10)),另一条水平线(坐标 (0,30))在右侧图中.

我该怎么做?

解决方案

您可以使用 FacetGrid.axes 获取 FacetGrid 中使用的轴列表,它返回使用的轴.然后,您可以使用这些轴执行所有正常的 matplotlib 操作,例如

A have produced a boxplot with Facetgrid in seaborn the following way

# Import the dataset
tips = sns.load_dataset("tips")

# Plot using FacetGrid, separated by smoke
plt.style.use('ggplot')
g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5)
g.map(sns.boxplot, "sex", "total_bill", palette='viridis', order=['Male', 'Female'])
plt.show()

I now want to draw to distinct horizontal lines in each of the plot. For instance one horizontal line (with coordinated (0,10)) only in the left-hand side plot and another horizontal line (with coordinates (0,30)) only in the right-hand side plot.

How can I go about doing that?

解决方案

You can get a list of axes used in the FacetGrid using FacetGrid.axes which returns the axes used. You can then do all of the normal matplotlib operations using these axes, such as axhline for horizontal lines, or plt.text for putting text on the axes:

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")

# Plot using Facegrid, separated by smoke
plt.style.use('ggplot')
g = sns.FacetGrid(tips, col="smoker", size=5, aspect=1.5)
g.map(sns.boxplot, "sex", "total_bill", palette='viridis', order=['Male', 'Female'])

ax1, ax2 = g.axes[0]

ax1.axhline(10, ls='--')
ax2.axhline(30, ls='--')

ax1.text(0.5,25, "Some text")
ax2.text(0.5,25, "Some text")

plt.show()

这篇关于在特定位置画一条线/在 seaborn 中注释 Facetgrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆