简体   繁体   English

如何为 seaborn 箱线图添加标题

[英]How to add title to seaborn boxplot

Seems pretty Googleable but haven't been able to find something online that works.看起来很Googleable,但无法在网上找到有效的东西。

I've tried both sns.boxplot('Day', 'Count', data= gg).title('lalala') and sns.boxplot('Day', 'Count', data= gg).suptitle('lalala') .我试过sns.boxplot('Day', 'Count', data= gg).title('lalala')sns.boxplot('Day', 'Count', data= gg).suptitle('lalala') None worked.没有工作。 I think it might be because I'm also working with matplotlib.我想这可能是因为我也在使用 matplotlib。

Seaborn box plot returns a matplotlib axes instance. Seaborn 框 plot 返回 matplotlib 轴实例。 Unlike pyplot itself, which has a method plt.title() , the corresponding argument for an axes is ax.set_title() .与具有方法plt.title()的 pyplot 本身不同,轴的相应参数是ax.set_title() Therefore you need to call因此,您需要致电

sns.boxplot('Day', 'Count', data= gg).set_title('lalala')

A complete example would be:一个完整的例子是:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
sns.boxplot(x=tips["total_bill"]).set_title("LaLaLa")

plt.show()

Of course you could also use the returned axes instance to make it more readable:当然,您也可以使用返回的轴实例使其更具可读性:

ax = sns.boxplot('Day', 'Count', data= gg)
ax.set_title('lalala')
ax.set_ylabel('lololo')

sns.boxplot() function returns Axes(matplotlib.axes.Axes) object. sns.boxplot() function 返回轴(matplotlib.axes.Axes)object。 please refer the documentation you can add title using 'set' method as below:请参考您可以使用“set”方法添加标题的文档,如下所示:

sns.boxplot('Day', 'Count', data=gg).set(title='lalala')

you can also add other parameters like xlabel, ylabel to the set method.您还可以在 set 方法中添加其他参数,例如 xlabel、ylabel。

sns.boxplot('Day', 'Count', data=gg).set(title='lalala', xlabel='its x_label', ylabel='its y_label')

There are some other methods as mentioned in the matplotlib.axes.Axes documentaion to add tile, legend and labels. matplotlib.axes.Axes 文档中提到了一些其他方法来添加图块、图例和标签。

Try adding this at the end of your code:尝试在代码末尾添加:

import matplotlib.pyplot as plt

plt.title('add title here')

For a single boxplot:对于单个箱线图:

import seaborn as sb
sb.boxplot(data=Array).set_title('Title')

For more boxplot in the same plot:有关同一 plot 中的更多箱线图:

import seaborn as sb
sb.boxplot(data=ArrayofArray).set_title('Title')

eg例如

import seaborn as sb
myarray=[78.195229, 59.104538, 19.884109, 25.941648, 72.234825, 82.313911]
sb.boxplot(data=myarray).set_title('myTitle')

.set_title('') can be used to add title to Seaborn Plot .set_title('')可用于为 Seaborn Plot 添加标题

import seaborn as sb
sb.boxplot().set_title('Title')

try this尝试这个

import seaborn as sns
g = sns.catplot(x='Day', y='Count', data=gg)
g.set_title('LaLa')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM