gpt4 book ai didi

Python:在 Facet_grid 中绘制堆积条形图

转载 作者:行者123 更新时间:2023-12-04 09:43:58 26 4
gpt4 key购买 nike

我有一个如下所示的数据框:

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

产生下图:
enter image description here
现在我想将条形图更改为堆叠条形图。我想使用分面网格来生成类似的布局。我使用以下代码生成图形但出现错误:
  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中生成堆积条形图?

最佳答案

正如某人在现已删除的评论/答案中提到的,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)
结果:
enter image description here Bokeh 中有更多关于样式的内容(带有示例)。 documentation .

关于Python:在 Facet_grid 中绘制堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62205823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com