python - 使用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')
解决方案
推荐阅读
- python - Python Pandas 匹配具有重叠坐标的行
- python - 如何使用 Pandas 从 CSV 文件夹生成自定义主数据框?
- java - 不支持 geode 客户端服务器版本 - 不支持序数为 100 的对等或客户端版本
- python - ThreadPoolExecutor 导致 Pyspark 池中的挂起
- html - 链接的“背景颜色”和“边框颜色”属性不使用 CSS 修改
- string - Powershell:在目录中的最新文件中提取两个字符串之间的数据
- filter - 在 FIORI 中过滤模型会导致“f.forEach 不是函数”
- python - facebook 营销 api,无法获取广告系列、广告和广告集草稿
- node.js - 有没有办法让不和谐机器人检测命令的作者是否在某个服务器上?不和谐.js
- react-native - 在 TopBar 自定义 React Native Navigation backButton