简体   繁体   中英

How to have two histograms but not stacked?

I would like to have the two histograms I have created but they should be next to each other and not on top of each other. Can you help me?

Code:

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

You can combine the two data rows in one single plot by adding the data rows in a list inside your plt.hist function.

Check out the documentation of the plt.hist funciton. It says, you "Input values, this takes either a single array or a sequence of arrays which are not required to be of the same length."

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

You would get then the following: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM