[英]Make a stacked bar plot from seaborn to matplotlib
I need some help making a set of stacked bar charts in python with matlibplot.我需要一些帮助,使用 matlibplot 在 python 中制作一组堆叠条形图。
Formally, my dataframe looks like this形式上,我的 dataframe 看起来像这样
plt.figure(figsize=(10, 14))
fig= plt.figure()
ax = sns.countplot(x="airlines",hue='typecode', data=trafic,
order=trafic.airlines.value_counts(ascending=False).iloc[:5].index,
hue_order=trafic.typecode.value_counts(ascending=False).iloc[:5].index,
)
ax.set(xlabel="Airlines code", ylabel='Count')
As written in order and hue_order, I want to isolate the 5 most present airlines and aircraft types in my database如 order 和 hue_order 所写,我想在我的数据库中隔离 5 个最常见的航空公司和飞机类型
I was advised to make a stacked bar plot to make a more presentable graph, only I don't see any functionality with Seaborn to make one, and I can't manage with matplotlib to plot it while respecting this idea of isolating the 5 airlines/aircraft types most present in my database I was advised to make a stacked bar plot to make a more presentable graph, only I don't see any functionality with Seaborn to make one, and I can't manage with matplotlib to plot it while respecting this idea of isolating the 5 airlines /我的数据库中最常见的飞机类型
Thanks for your help!谢谢你的帮助!
The following code uses seaborn's countplot with dodge=False
.以下代码使用 seaborn 的 countplot 和
dodge=False
。 This places all bars belonging to the same airline one on top of the other.这会将属于同一航空公司的所有酒吧放在另一个之上。 In a next step, all bars are moved up to stack them:
在下一步中,所有条都向上移动以堆叠它们:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
sns.set()
np.random.seed(123)
trafic = pd.DataFrame({'airlines': np.random.choice([*'abcdefghij'], 500),
'typecode': np.random.choice([*'qrstuvwxyz'], 500)})
fig = plt.figure(figsize=(10, 5))
ax = sns.countplot(x="airlines", hue='typecode', palette='rocket', dodge=False, data=trafic,
order=trafic.airlines.value_counts(ascending=False).iloc[:5].index,
hue_order=trafic.typecode.value_counts(ascending=False).iloc[:5].index)
ax.set(xlabel="Airlines code", ylabel='Count')
bottoms = {}
for bars in ax.containers:
for bar in bars:
x, y = bar.get_xy()
h = bar.get_height()
if x in bottoms:
bar.set_y(bottoms[x])
bottoms[x] += h
else:
bottoms[x] = h
ax.relim() # the plot limits need to be updated with the moved bars
ax.autoscale()
plt.show()
Note that the airlines are sorted on their total airplanes, not on their total for the 5 overall most frequent airplane types.请注意,航空公司是按飞机总数排序的,而不是按最常见的 5 种飞机类型的总数排序。
PS: In the question's code, plt.figure()
is called twice. PS:在问题的代码中,
plt.figure()
被调用了两次。 That first creates an empty figure with the given figsize, and then a new figure with a default figsize.这首先创建一个具有给定 figsize 的空图形,然后创建一个具有默认 figsize 的新图形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.