Matplotlib 使用「;“堆叠”;在Seaborn FactoryPlot中进行设计

Matplotlib 使用「;“堆叠”;在Seaborn FactoryPlot中进行设计,matplotlib,seaborn,Matplotlib,Seaborn,我使用seaborn Factorplot(kind=bar)在2个“类别”中总共表示6条(每个类别3条)。我想通过使用堆叠设计来增强这个factorplot,也就是说,我想用每个条的“子组件”来表示每个条。 我知道这对于barplot是可能的,但是对于FactoryPlot也是可能的吗?Randy Zwitch如何在seaborn中创建堆叠条形图 解决方案是将堆叠条形图视为同一图形上的多个重叠图,以便条形图的底部段位于前面,并掩盖后续段的最低部分 引用他的博客: import pandas a

我使用seaborn Factorplot(kind=bar)在2个“类别”中总共表示6条(每个类别3条)。我想通过使用堆叠设计来增强这个factorplot,也就是说,我想用每个条的“子组件”来表示每个条。 我知道这对于barplot是可能的,但是对于FactoryPlot也是可能的吗?

Randy 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)

展示一些演示数据结构的代码,以及您已经尝试过的内容。