简体   繁体   English

用 Matplotlib 和 Python 绘制两个直方图

[英]Plot two Histograms with Matplotlib and Python

I have two lists of numbers:我有两个数字列表:

list_1 = [1,2,3,4,5,1,2,3,4,5,6,3,4,5,1,3,4,5,4,5,6,8,9,12,3,3,3,4,3,4,5,6,5,6,7,8,9,5,3,2,4,5,2,3,4,11,13,4,5,3,5,6,7,11,13,3,4,5,4,5]
list_2 = [4,5,6,7,8,9,4,5,6,7,8,9,5,6,7,8,9,6,7,8,9,12,15,16,11,12,7,8,9,7,8,9,5,6,7,8,9,7,8,9,8,9,11,10,12,16,7,8,9,10,10,8,9,8,9,10,10,10,15,16,19]

I want to plot two histograms with Python and Matplotlib so that i get result like this:我想用 Python 和 Matplotlib 绘制两个直方图,以便得到如下结果:

在此处输入图片说明

I need the line histogram, and I do not know how to plot it.我需要线直方图,但我不知道如何绘制它。 I know how to make bar histogram, but I want to have line histogram so that I can see intersection of this two histograms.我知道如何制作条形直方图,但我想要线条直方图,以便我可以看到这两个直方图的交集。

plt.hist(list_1, bins = 10)
plt.hist(list_2, bins = 10)
plt.show()

The Seaborn Library can help you: Seaborn图书馆可以帮助您:

import matplotlib.pyplot as plt
import seaborn as sns


# Your Data
list_1 = [1,2,3,4,5,1,2,3,4,5,6,3,4,5,1,3,4,5,4,5,6,8,9,12,3,3,3,4,3,4,5,6,5,6,7,8,9,5,3,2,4,5,2,3,4,11,13,4,5,3,5,6,7,11,13,3,4,5,4,5]
list_2 = [4,5,6,7,8,9,4,5,6,7,8,9,5,6,7,8,9,6,7,8,9,12,15,16,11,12,7,8,9,7,8,9,5,6,7,8,9,7,8,9,8,9,11,10,12,16,7,8,9,10,10,8,9,8,9,10,10,10,15,16,19]

# Creating a displot
fig = plt.figure(figsize=(15,5))
ax = fig.add_subplot(111)

sns.distplot(list_1, kde=True, ax = ax, hist=False, bins = 10)
sns.distplot(list_2, kde=True, ax = ax, hist=False, bins = 10)

plt.show()

结果图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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