Seaborn:标题和副标题放置 [英] Seaborn: title and subtitle placement

查看:177
本文介绍了Seaborn:标题和副标题放置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想创建一个散点图,其标题、副标题、颜色对应于特定变量,大小对应于另一个变量.我想显示颜色图例而不是大小.这是我目前所拥有的:

I'd like to create a scatterplot with a title, subtitle, colours corresponding to a specific variable and size corresponding to another variable. I want to display the colour legend but not the size. Here is what I have so far:

# imports
import seaborn as sns
import matplotlib
from matplotlib import style
import matplotlib.pyplot as plt

# parameters
matplotlib.rcParams['font.family'] = "roboto"
style.use('fivethirtyeight')

# load data
iris = sns.load_dataset('iris')

# plot
ax = sns.relplot(
    'sepal_length',
    'sepal_width',
    hue='species',
    size='petal_width',
    alpha=0.75,
    kind="scatter",
    legend=False,
    data=iris
)

# make adjustments
ax.set_axis_labels(x_var='Sepal Length', y_var='Sepal Width')
plt.text(x=4.7, y=4.7, s='Sepal Length vs Width', fontsize=16, weight='bold')
plt.text(x=4.7, y=4.6, s='The size of each point corresponds to sepal width', fontsize=8, alpha=0.75)
plt.show()

输出:

这是我的问题:

1) 有没有更好的方法来设置字幕?我使用 ax.suptitle("blah", y=1.05) 尝试了这个,但它最终位于图的范围之外.我不喜欢我必须为我的标题/副标题设置 x 和 y 坐标.

1) Is there a better way to set a subtitle? I tried this using ax.suptitle("blah", y=1.05) but it ends up sitting outside the scope of the figure. I don't like that I have to set x and y coordinates for my title/subtitle.

2) 有没有办法让我在不显示尺寸图例的情况下显示颜色图例?我还希望能够在情节下方(或外部)显示此图例.如果你能回答这个问题,我会更改这篇文章的标题,将你的答案标记为完整并创建另一个关于标题和副标题的问题

2) Is there a way for me to display the colour legend without showing the size legend? I would also like to be able to display this legend below the plot (or outside it). if you can answer that question, I'll change the title of this post, mark your answer as complete and create another question about the titles and subtitles

非常感谢!

推荐答案

使用 scatterplot() 可以更轻松地操作图例.如果您使用 legend='brief,那么您将获得此图例:

Using scatterplot() makes it easier to manipulate the legend. If you use legend='brief then you'll get this legend:

您可以使用以下方法获取用于创建此图例的艺术家和标签:

You can get the artists and the labels used to create this legend using:

h,l = ax.get_legend_handles_labels()

由于您只需要颜色信息,而不是大小,因此解决方案只是使用前半部分艺术家重​​新创建图例

since you only want the color info, and not the size, the solution is simply to recreate the legend using the first half of the artists

ax.legend(h[:4],l[:4])

完整代码:

matplotlib.style.use('fivethirtyeight')
# load data
iris = sns.load_dataset('iris')

# plot
fig, ax = plt.subplots(figsize=(7,5))
sns.scatterplot(
    'sepal_length',
    'sepal_width',
    hue='species',
    size='petal_width',
    alpha=0.75,
    legend='brief',
    data=iris,
    ax=ax
)

# make adjustments
ax.set_xlabel('Sepal Length')
ax.set_ylabel('Sepal Width')

ax.text(x=0.5, y=1.1, s='Sepal Length vs Width', fontsize=16, weight='bold', ha='center', va='bottom', transform=ax.transAxes)
ax.text(x=0.5, y=1.05, s='The size of each point corresponds to sepal width', fontsize=8, alpha=0.75, ha='center', va='bottom', transform=ax.transAxes)

h,l = ax.get_legend_handles_labels()
ax.legend(h[:4],l[:4], bbox_to_anchor=(1.05, 1), loc=2)

fig.tight_layout()
plt.show()

这篇关于Seaborn:标题和副标题放置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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