![](/img/trans.png)
[英]How to draw two stacked histograms side-by-side with Matplotlib/Seaborn
[英]How to have two histograms but not stacked?
我想擁有我創建的兩個直方圖,但它們應該彼此相鄰,而不是彼此重疊。 你能幫助我嗎?
代碼:
import pandas as pd
members = pd.read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/members.csv")
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(palette="Reds_r")
plt.figure(figsize=(15,10))
members_injury_height = members["injury_height_metres"]
members_death_height = members["death_height_metres"]
members_injury_height.hist(density=True,alpha = 0.5,bins = 20)
members_death_height.hist(density=True, alpha = 0.2 ,bins = 20)
plt.xlabel("Hauteurs")
plt.ylabel("% de membres morts/blessés")
plt.title("Répartition des hauteurs auxquelles des membres se sont blessés ou sont morts")
fond = plt.gca()
fond.set_facecolor('whitesmoke')
plt.gca().legend(('blessés', 'morts'))
通過在 plt.hist function 內的列表中添加數據行,您可以將兩個數據行合並到一個plt.hist
中。
查看plt.hist
的文檔。 它說,您“輸入值,這需要單個數組或 arrays 序列,它們不需要具有相同的長度。”
sns.set(palette="Reds_r")
plt.figure(figsize=(15,10))
members_injury_height = members["injury_height_metres"]
members_death_height = members["death_height_metres"]
plt.hist([members_injury_height, members_death_height], 20)
plt.legend(loc='upper right')
plt.xlabel("Hauteurs")
plt.ylabel("% de membres morts/blessés")
plt.title("Répartition des hauteurs auxquelles des membres se sont blessés ou sont morts")
fond = plt.gca()
fond.set_facecolor('whitesmoke')
plt.gca().legend(('blessés', 'morts'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.