A 通过以下方式在seaborn中使用Facetgrid生成了箱线图
# 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()
我现在想在每个图中绘制不同的水平线。例如一条水平线(坐标为 (0,10))only在左侧图中和另一条水平线(坐标为(0,30))only在右侧图中。
我该如何去做呢?
您可以使用以下命令获取 FacetGrid 中使用的轴列表FacetGrid.axes
它返回使用的轴。然后,您可以使用这些轴执行所有正常的 matplotlib 操作,例如axhline https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.axhline.html对于水平线,或plt.text https://matplotlib.org/api/_as_gen/matplotlib.pyplot.text.html将文本放在轴上:
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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)