问题描述
我使用 seaborn Factorplot (kind=bar) 来表示 2 个类别"中的总共 6 个条形(每个类别 3 个条形).我想通过使用堆叠设计来增强此因子图,也就是说,我想通过其子组件"来表示每个条.我知道这对于barplot是可能的,但是对于factorplot也可能吗?
I use a seaborn Factorplot (kind=bar) to represent in total 6 bars in 2 "categories" (3 bars for each category). I would like to enhance this factorplot by using the stacked design, that is I want to represent each bar by its "subcomponents".I know that this is possible for barplot but is it also possible for factorplot?
推荐答案
Randy Zwitch 解释如何在seaborn中创建堆积的条形图.
Randy Zwitch explains how to create a stacked bar chart in seaborn.
解决方案是将堆叠的条形图视为同一图形上的多个重叠图,因此条形图的底部位于前面,而遮盖了后续分段的最低部分.
The solution is to think of a stacked bar chart as multiple, overlaying, plots on the same graph so the bottom segment of the bars are at the front and obscure the lowest parts of the subsequent segments.
引用他的博客:
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)
这篇关于使用“堆叠"Seaborn Factorplot 中的设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!