简体   繁体   English

在 seaborn 中使用 catplot 时更改 x-labels 和宽度

[英]Changing x-labels and width while using catplot in seaborn

I have a sample dataset as follows;我有一个示例数据集如下;

pd.DataFrame({'Day_Duration':['Evening','Evening','Evening','Evening','Evening','Morning','Morning','Morning',
                         'Morning','Morning','Night','Night','Night','Night','Night','Noon','Noon','Noon',
                         'Noon','Noon'],'place_category':['Other','Italian','Japanese','Chinese','Burger',
                        'Other','Juice Bar','Donut','Bakery','American','Other','Italian','Japanese','Burger',\
                        'American','Other','Italian','Burger','American','Salad'],'Percent_delivery':[14.03,10.61,9.25,8.19,6.89,19.58,10.18,9.14,8.36,6.53,13.60,8.42,\
                                           8.22,7.66,6.67,17.71,10.62,8.44,8.33,7.50]})

The goal is to draw faceted barplot with Day_duration serving as facets, hence 4 facets in total.目标是绘制以Day_duration为刻面的刻面条形图,因此总共有 4 个刻面。 I used the following code to achieve the same,我使用以下代码来实现相同的目标,

import seaborn as sns
#g = sns.FacetGrid(top5_places, col="Day_Duration")
g=sns.catplot(x="place_category", y="Percent_delivery", hue='place_category',col='Day_Duration',\
                 data=top5_places,ci=None,kind='bar',height=4, aspect=.7)
g.set_xticklabels(rotation=90)

Attached is the figure I got;附上我得到的图;

Can I kindly get help with 2 things, first is it possible to get only 5 values on the x-axis for each facet(rather than seeing all the values for each facet), second, is there a way to make the bars a bit wider.我可以在两件事上寻求帮助吗,首先是否可以在每个方面的 x 轴上仅获得 5 个值(而不是查看每个方面的所有值),第二,有没有办法使条形有点更宽的。 Help is appreciated.帮助表示赞赏。

  • Because you're using hue the api applies a unique color to each value of place_category , but it also expects each category to be in the plot, as shown in your image.因为您使用的是hue ,所以 api 将唯一的颜色应用于place_category的每个值,但它也希望每个类别都在 plot 中,如图所示。
    • The final figure is a FacetGrid.最后一个图是一个 FacetGrid。 Using subplot is the manual way of creating one.使用subplot是创建一个的手动方式。
  • In order to plot only the top n categories for each Day_Duration , each plot will need to be done individually, with a custom color map.为了 plot 只有每个 Day_Duration 的前n类别,每个Day_Duration都需要单独完成,并使用自定义颜色 map。
  • cmap is a dictionary with place categories as keys and colors as values. cmap是一个字典,其中地点类别作为键,colors 作为值。 It's used so there will be one legend and each category will be colored the same for each plot.使用它,因此将有一个图例,并且对于每个 plot,每个类别的颜色都相同。
    • Because we're not using the legend automatically generated by the plot, one needs to be created manually.因为我们没有使用 plot 自动生成的图例,所以需要手动创建一个。
  • patches uses Patch to create each item in the legend. patches使用Patch创建图例中的每个项目。 (eg the rectangle, associated with color and name). (例如矩形,与颜色和名称相关联)。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.patches import Patch


# create a color map for unique values or place
place_cat = df.place_category.unique()
colors = sns.color_palette('husl', n_colors=10)
cmap = dict(zip(place_cat, colors))

# plot a subplot for each Day_Duration
plt.figure(figsize=(16, 6))
for i, tod in enumerate(df.Day_Duration.unique(), 1):
    data = df[df.Day_Duration == tod].sort_values(['Percent_delivery'], ascending=False)
    plt.subplot(1, 4, i)
    p = sns.barplot(x='place_category', y='Percent_delivery', data=data, hue='place_category', palette=cmap)
    p.legend_.remove()
    plt.xticks(rotation=90)
    plt.title(f'Day Duration: {tod}')

plt.tight_layout()
patches = [Patch(color=v, label=k) for k, v in cmap.items()]
plt.legend(handles=patches, bbox_to_anchor=(1.04, 0.5), loc='center left', borderaxespad=0)
plt.show()

在此处输入图像描述

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

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