首页 > 解决方案 > 使用seaborn绘制多组条形图+线图

问题描述

我正在尝试使用 seaborn 从数据框中绘制条形图 + 线图,如下所示(我已经成功绘制了一个):

在此处输入图像描述

在这里您可以找到用于生成它的数据框 =>数据框

我想要实现的是这种图表(我已经成功创建),但对特定产品篮子内的所有可能的产品系列重复('family'是'product basket'的向下钻取级别)

非常感谢任何会帮助我的人。我尝试了一些循环,但可能我做错了。

这是我用来绘制上图的代码:

#select a family
df_perc2=df_perc.query('family=="WORKWEAR & PROTECTIVE CLOTHING"')

#set common axis
fig, ax = plt.subplots()
ax_twin = ax.twinx()

#set boxplot general aspect
fig = plt.gcf()
fig.set_size_inches(30, 10)
sns.set_style("white")

#ship_to_count bars
barplot = sns.barplot(data=df_perc2, 
                  x = 'orders_count', 
                  hue = 'SF_type', 
                  y = 'ship_to_perc',
                  palette = "Set2",
                  ax = ax)
#cumulative % line
lineplot = sns.pointplot(data = df_perc2,
                     x = 'orders_count',
                     hue = 'SF_type',
                     y = 'running_perc',
                     palette = "Set2",
                     marker ='o',
                     ax = ax_twin,
                     legend = False)

#set tick stiles for x and y axis
barplot.set_xticklabels(barplot.get_xmajorticklabels(), fontsize = 18)
barplot.set_yticklabels(barplot.get_yticks().round(2), size = 18)
lineplot.set_yticklabels(lineplot.get_yticks().round(2), size = 18)

#set dynamic title
barplot.set_title('% Ship Tos by # orders for '+''.join(df_perc2['product_basket'].unique())
              +
              ' - '
              +
              ''.join(df_perc2['family'].unique()), fontdict = { 'fontsize': 30}, y = 1.05)


barplot.get_legend().remove()

#set constant line at 90%
plt.axhline(y=0.9, color='g', ls=':', lw=4, label='90th percentile')
lineplot.legend(loc='center right',fontsize='22')

标签: pythonseabornbar-chart

解决方案


推荐阅读