簡體   English   中英

用 Matplotlib 和 Python 繪制兩個直方圖

[英]Plot two Histograms with Matplotlib and Python

我有兩個數字列表:

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]

我想用 Python 和 Matplotlib 繪制兩個直方圖,以便得到如下結果:

在此處輸入圖片說明

我需要線直方圖,但我不知道如何繪制它。 我知道如何制作條形直方圖,但我想要線條直方圖,以便我可以看到這兩個直方圖的交集。

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

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