pandas 如何为seaborn boxplot添加标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42406233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add title to seaborn boxplot
提问by itstoocold
Seems pretty Googleable but haven't been able to find something online that works.
看起来很谷歌,但一直无法在网上找到有效的东西。
I've tried both sns.boxplot('Day', 'Count', data= gg).title('lalala')
and sns.boxplot('Day', 'Count', data= gg).suptitle('lalala')
. None worked. I think it might be because I'm also working with matplotlib.
sns.boxplot('Day', 'Count', data= gg).title('lalala')
和都试过了sns.boxplot('Day', 'Count', data= gg).suptitle('lalala')
。没有一个工作。我想这可能是因为我也在使用 matplotlib。
回答by ImportanceOfBeingErnest
Seaborn box plot returns a matplotlib axes instance. Unlike pyplot itself, which has a method plt.title()
, the corresponding argument for an axes is ax.set_title()
. Therefore you need to call
Seaborn 箱线图返回一个 matplotlib 轴实例。与 pyplot 本身不同,它有一个方法plt.title()
,轴的相应参数是ax.set_title()
。因此你需要打电话
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')
回答by Stefano Potter
Try adding this at the end of your code:
尝试在代码末尾添加:
import matplotlib.pyplot as plt
plt.title('add title here')
回答by akhil penta
sns.boxplot() function returns Axes(matplotlib.axes.Axes) object. please refer the documentationyou can add title using 'set' method as below:
sns.boxplot() 函数返回 Axes(matplotlib.axes.Axes) 对象。请参阅 您可以使用“设置”方法添加标题的文档,如下所示:
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 documentaionto add tile, legend and labels.
matplotlib.axes.Axes 文档中提到了一些其他方法来添加平铺、图例和标签。
回答by Kranthi Kumar Valaboju
.set_title('')can be used to add title to Seaborn Plot
.set_title('')可用于为 Seaborn Plot 添加标题
import seaborn as sb
sb.boxplot().set_title('Title')
回答by Shrm
For a single boxplot:
对于单个箱线图:
import seaborn as sb
sb.boxplot(data=Array).set_title('Title')
For more boxplot in the same plot:
对于同一图中的更多箱线图:
import seaborn as sb
sb.boxplot(data=ArrayofArray).set_title('Title')
e.g.
例如
import seaborn as sb
myarray=[78.195229, 59.104538, 19.884109, 25.941648, 72.234825, 82.313911]
sb.boxplot(data=myarray).set_title('myTitle')