我有一个如下所示的数据框:
Direction ID RY Value Part_of_prog
North 1 2016 29 0
North 2 2016 25 1
North 3 2016 25 0
North 4 2016 22 1
North 4 2017 22 1
North 4 2017 24 1
North 4 2017 25 0
North 4 2017 26 0
North 5 2018 26 0
North 5 2018 26 1
North 5 2018 20 1
North 5 2018 22 0
South 6 2018 22 0
South 6 2018 22 1
South 6 2018 24 1
South 6 2018 24 0
South 7 2017 24 0
South 7 2017 24 1
South 7 2017 19 1
South 7 2017 18 0
South 7 2016 13 0
South 7 2016 13 1
South 7 2016 14 1
South 7 2016 19 0
East 1 2016 29 0
East 2 2016 15 1
East 3 2016 25 0
East 4 2016 22 1
East 4 2017 22 1
East 4 2017 14 1
East 4 2017 25 0
East 4 2017 26 0
East 5 2018 16 0
East 5 2018 26 1
East 5 2018 10 1
East 5 2018 22 0
我正在使用下面的代码使用seaborn在Facet网格中绘制条形图:
g = sns.FacetGrid(df,col='Direction')
g = g.map_dataframe(sns.barplot,"RY","Value",hue='Part_of_prog',ci=None,palette = sns.color_palette("bright"))
for ax in g.axes.ravel():
ax.legend()
plt.show()
产生以下图形: 现在,我想将条形图更改为堆积条形图。我想使用构面网格生成类似的布局。我使用以下代码生成图形,但出现错误:
g = sns.FacetGrid(df,col='Direction')
g = g.map_dataframe(df[['Value','RY']].plot.bar(stacked=True),hue='Part_of_prog',palette = sns.color_palette("bright"))
for ax in g.axes.ravel():
ax.legend()
plt.show()
有人可以指导我如何在facegrid中生成堆积条形图吗?
答案 0 :(得分:2)
正如在现在已删除的评论/答案中提到的那样,seaborn
是一个自以为是的数据可视化库,这意味着它可以轻松地创建某些(主要是统计的)图形类型,而不是通用的“构建自己的工具”。根据其作者说,堆积的条形不属于scope。
因此,我建议您创建使用Bokeh
之后的图形类型。数据将需要以一种稍微的不同方式进行结构化(必须将堆栈变量旋转到列中),并且还有一点要编写的代码,但这就是这样做的代价。更大的灵活性。
首先,进行数据操作:
df["Part_of_prog"] = df["Part_of_prog"].astype(str)
df["RY"] = df["RY"].astype(str)
wide_df = (df
.pivot_table(index=["Direction", "RY"], columns="Part_of_prog", values="Value", aggfunc=sum)
.reset_index()
.rename_axis(None, axis="columns")
)
现在是可视化部分:
from bokeh.io import output_notebook, output_file, show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, CDSView, BooleanFilter, Range1d
from bokeh.layouts import row
output_notebook() # for Jupyter Notebooks
# shared ranges for the 3 plots
x_range = df["RY"].unique()
y_range = Range1d(0, 110)
colors = ["#c9d9d3", "#718dbf"]
# shared source for all plots to allow linked brushing/panning if you need it
source = ColumnDataSource(wide_df)
# filtered views for each of the facets
east_view = CDSView(source=source, filters=[BooleanFilter(wide_df["Direction"] == "East")])
north_view = CDSView(source=source, filters=[BooleanFilter(wide_df["Direction"] == "North")])
south_view = CDSView(source=source, filters=[BooleanFilter(wide_df["Direction"] == "South")])
# plots are broadly similar, but we'll use a facet object to modify things like titles
def generate_facet_plot(facet):
p = figure(x_range=x_range, y_range=y_range, plot_height=250, plot_width=200,
title=facet["title"], toolbar_location=None, tools="")
p.vbar_stack(stackers=["0","1"], x="RY", color=colors, width=0.9,
source=source, view=facet["view"])
p.yaxis.visible = facet["y_axis_visible"]
p.xgrid.visible = False
p.y_range.start = 0
return p
# our configuration object for facets
facets = {
"east": {
"title": "East",
"view" : east_view,
"y_axis_visible": True
},
"north": {
"title": "North",
"view" : north_view,
"y_axis_visible": False
},
"south": {
"title": "South",
"view" : south_view,
"y_axis_visible": False
},
}
plots = []
for facet in facets.keys():
plots.append(generate_facet_plot(facets[facet]))
# row layout; other options are available
p = row(plots)
show(p)
结果:
Bokeh
documentation中有许多样式(带有示例)。