为什么我的图表不是每个月都显示? Facetgrid,Seaborn
Why is my graph not showing every Month? Facetgrid, Seaborn
我的样本数据集如下:
dataset = {
"ClientId": [
10, 20, 20, 20, 10, 5, 3, 7, 5, 20, 12, 5,
3, 20, 5, 8, 10, 9, 7, 20, 21, 5, 3, 10,
],
"Year": [
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
],
"Month": [
"Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr",
"Mai", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
],
"Sector": [
"A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C",
"A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C",
],
"Quantity": [
100, 50, 25, 30, 40, 50, 200, 600, 20, 40, 100, 20, 50,
400, 250, 300, 125, 75, 90, 10, 225, 550, 450, 55,
],
"Regions": [
"Europe", "Asia", "Africa", "Africa", "Other", "Asia",
"Africa", "Other", "America", "America", "Europe",
"Europe", "Other", "Europe", "Asia", "Africa", "Asia",
"Europe", "Other", "Africa", "Europe", "Asia", "Africa",
"Europe",
],
}
df = pd.DataFrame(data=dataset)
我想为每个年份和部门制作一个 Facetgrid
条形图。
我已经成功地创建了情节:
g = sns.FacetGrid(df, col='Year', row='Sector', margin_titles=True)
g.map_dataframe(sns.barplot, x='Month', y='Quantity', color='orange')
g.set_axis_labels('Months','Quantity')
问题: 条形图不是每个月都显示,只有几个。我想要的是每月看到一个小节,但我被卡住了。
图表目前看起来像这样:
您可以通过向 map_dataframe
提供列 order
来强制 seaborn 使用所有列:
g = sns.FacetGrid(df, col="Year", row="Sector", margin_titles=True)
g.map_dataframe(
sns.barplot, x="Month", y="Quantity", color="orange", order=df["Month"].unique()
)
g.set_axis_labels("Months", "Quantity")
g.set_xticklabels(rotation=90)
或者,我认为更好的选择是像这样使用 sns.catplot
。这仍然是 returns 一个 facetgrid,但不需要额外的调整。
g = sns.catplot(
x="Month",
y="Quantity",
col="Year",
row="Sector",
data=df,
kind="bar",
height=2.5,
aspect=0.8,
color="orange",
margin_titles=True,
)
g.set_xticklabels(rotation=90)
产生:
我的样本数据集如下:
dataset = {
"ClientId": [
10, 20, 20, 20, 10, 5, 3, 7, 5, 20, 12, 5,
3, 20, 5, 8, 10, 9, 7, 20, 21, 5, 3, 10,
],
"Year": [
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019,
2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
],
"Month": [
"Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr",
"Mai", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
],
"Sector": [
"A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C",
"A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C",
],
"Quantity": [
100, 50, 25, 30, 40, 50, 200, 600, 20, 40, 100, 20, 50,
400, 250, 300, 125, 75, 90, 10, 225, 550, 450, 55,
],
"Regions": [
"Europe", "Asia", "Africa", "Africa", "Other", "Asia",
"Africa", "Other", "America", "America", "Europe",
"Europe", "Other", "Europe", "Asia", "Africa", "Asia",
"Europe", "Other", "Africa", "Europe", "Asia", "Africa",
"Europe",
],
}
df = pd.DataFrame(data=dataset)
我想为每个年份和部门制作一个 Facetgrid
条形图。
我已经成功地创建了情节:
g = sns.FacetGrid(df, col='Year', row='Sector', margin_titles=True)
g.map_dataframe(sns.barplot, x='Month', y='Quantity', color='orange')
g.set_axis_labels('Months','Quantity')
问题: 条形图不是每个月都显示,只有几个。我想要的是每月看到一个小节,但我被卡住了。
图表目前看起来像这样:
您可以通过向 map_dataframe
提供列 order
来强制 seaborn 使用所有列:
g = sns.FacetGrid(df, col="Year", row="Sector", margin_titles=True)
g.map_dataframe(
sns.barplot, x="Month", y="Quantity", color="orange", order=df["Month"].unique()
)
g.set_axis_labels("Months", "Quantity")
g.set_xticklabels(rotation=90)
或者,我认为更好的选择是像这样使用 sns.catplot
。这仍然是 returns 一个 facetgrid,但不需要额外的调整。
g = sns.catplot(
x="Month",
y="Quantity",
col="Year",
row="Sector",
data=df,
kind="bar",
height=2.5,
aspect=0.8,
color="orange",
margin_titles=True,
)
g.set_xticklabels(rotation=90)
产生: