使用“堆叠”设计Seaborn Factorplot(Use “stacked” design in

2019-10-21 00:17发布

我用seaborn Factorplot(种类=巴)总共6条,以表示2“类别”(3巴为每个类别)。 我想通过堆叠设计,以加强这方面的factorplot,那就是我想通过它的“子”来表示每个酒吧。 我知道这是可能的,但barplot是不是也可以factorplot?

Answer 1:

兰迪Zwitch 解释了如何创建seaborn堆积条形图。

该解决方案是考虑一个堆积条形图的为多个,重叠,图上相同的图,从而所述杆的底部段是在前面和模糊后续段的最低部分。

从他的博客引用:

import pandas as pd
from matplotlib import pyplot as plt
import matplotlib as mpl
import seaborn as sns
%matplotlib inline

#Read in data & create total column
stacked_bar_data = pd.read_csv("C:\stacked_bar.csv")
stacked_bar_data["total"] = stacked_bar_data.Series1 + stacked_bar_data.Series2

#Set general plot properties
sns.set_style("white")
sns.set_context({"figure.figsize": (24, 10)})

#Plot 1 - background - "total" (top) series
sns.barplot(x = stacked_bar_data.Group, y = stacked_bar_data.total, color = "red")

#Plot 2 - overlay - "bottom" series
bottom_plot = sns.barplot(x = stacked_bar_data.Group, y = stacked_bar_data.Series1, color = "#0000A3")


topbar = plt.Rectangle((0,0),1,1,fc="red", edgecolor = 'none')
bottombar = plt.Rectangle((0,0),1,1,fc='#0000A3',  edgecolor = 'none')
l = plt.legend([bottombar, topbar], ['Bottom Bar', 'Top Bar'], loc=1, ncol = 2, prop={'size':16})
l.draw_frame(False)

#Optional code - Make plot look nicer
sns.despine(left=True)
bottom_plot.set_ylabel("Y-axis label")
bottom_plot.set_xlabel("X-axis label")

#Set fonts to consistent 16pt size
for item in ([bottom_plot.xaxis.label, bottom_plot.yaxis.label] +
             bottom_plot.get_xticklabels() + bottom_plot.get_yticklabels()):
    item.set_fontsize(16)


文章来源: Use “stacked” design in Seaborn Factorplot