📜  如何在 Matplotlib 中一起绘制两个直方图?

📅  最后修改于: 2022-05-13 01:55:37.753000             🧑  作者: Mango

如何在 Matplotlib 中一起绘制两个直方图?

先决条件

  • Matplotlib

直方图是将分组数据点组织到指定范围内的图形表示。创建直方图提供了数据分布的可视化表示。通过使用直方图,我们可以将大量数据及其频率表示为一个连续图。

使用的函数

为了在 Matplotlib 中创建直方图,我们使用属于 pyplot 模块的 hist()函数。为了将两个直方图绘制在一起,我们必须通过提供一些设置来分别对两个数据集使用 hist()函数。

句法:

范围:

ParameterDescription
xArray or dataset for plotting the histogram
binsInteger values or sequence, used for intervals
edgecolor or ecSets the edge color of histogram bars
colorSets the bar color of histogram bars
labelUsed to represent the label of the histogram it is of string type.
alphaUsed for setting amount of transparency.
labelUsed to represent the name or label of the histogram.

方法

  • 导入模块
  • 为两个数据集创建或加载数据
  • 分别用两个数据帧绘制直方图
  • 将它们绘制在一起

示例 1:

Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
  
# generating two series of random 
# values using numpy random module 
# of shape (500,1)
series1 = np.random.randn(500, 1)
series2 = np.random.randn(400, 1)
  
# plotting first histogram
plt.hist(series1)
  
# plotting second histogram
plt.hist(series2)
  
# Showing the plot using plt.show()
plt.show()


Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
from numpy.lib.histograms import histogram
  
# generating two series of random values 
# using numpy random module of shape (500,1)
series1 = np.random.randn(500, 1)
series2 = np.random.randn(400, 1)
  
# plotting first histogram
plt.hist(series1, label='series1', alpha=.8, edgecolor='red')
  
# plotting second histogram
plt.hist(series2, label='series2', alpha=0.7, edgecolor='yellow')
plt.legend()
  
# Showing the plot using plt.show()
plt.show()


Python
# importing libraries
import matplotlib.pyplot as plt
  
# giving two age groups data
age_g1 = [1, 3, 5, 10, 15, 17, 18, 16, 19, 21,
          23, 28, 30, 31, 33, 38, 32, 40, 45, 
          43, 49, 55, 53, 63, 66, 85, 80, 57, 
          75, 93, 95]
  
age_g2 = [6, 4, 15, 17, 19, 21, 28, 23, 31, 36,
          39, 32, 50, 56, 59, 74, 79, 34, 98, 97,
          95, 67, 69, 92, 45, 55, 77, 76, 85]
  
# plotting first histogram
plt.hist(age_g1, label='Age group1', alpha=.7, edgecolor='red')
  
# plotting second histogram
plt.hist(age_g2, label='Age group2', alpha=0.7, edgecolor='yellow')
plt.legend()
  
# Showing the plot using plt.show()
plt.show()


Python
# importing libraries
import matplotlib.pyplot as plt
  
# giving two age groups data
age_g1 = [1, 3, 5, 10, 15, 17, 18, 16, 19,
          21, 23, 28, 30, 31, 33, 38, 32, 
          40, 45, 43, 49, 55, 53, 63, 66, 
          85, 80, 57, 75, 93, 95]
  
age_g2 = [6, 4, 15, 17, 19, 21, 28, 23, 31, 
          36, 39, 32, 50, 56, 59, 74, 79, 34, 
          98, 97, 95, 67, 69, 92, 45, 55, 77,
          76, 85]
  
# plotting first histogram
plt.hist(age_g1, label='Age group1', bins=14, alpha=.7, edgecolor='red')
  
# plotting second histogram
plt.hist(age_g2, label="Age group2", bins=14, alpha=.7, edgecolor='yellow')
plt.legend()
  
# Showing the plot using plt.show()
plt.show()


Python
# importing libraries
import matplotlib.pyplot as plt
  
# giving two age groups data
age_g1 = [1, 3, 5, 10, 15, 17, 18, 16, 19, 21,
          23, 28, 30, 31, 33, 38, 32, 40, 45, 
          43, 49, 55, 53, 63, 66, 85, 80, 57, 
          75, 93, 95]
  
age_g2 = [6, 4, 15, 17, 19, 21, 28, 23, 31, 36,
          39, 32, 50, 56, 59, 74, 79, 34, 98, 97,
          95, 67, 69, 92, 45, 55, 77, 76, 85]
  
# plotting first histogram
plt.hist(age_g1, label='Age group1', alpha=.7, color='red')
  
# plotting second histogram
plt.hist(age_g2, label="Age group2", alpha=.5,
         edgecolor='black', color='yellow')
plt.legend()
  
# Showing the plot using plt.show()
plt.show()


输出:

示例 2:

Python

# importing libraries
import matplotlib.pyplot as plt
import numpy as np
from numpy.lib.histograms import histogram
  
# generating two series of random values 
# using numpy random module of shape (500,1)
series1 = np.random.randn(500, 1)
series2 = np.random.randn(400, 1)
  
# plotting first histogram
plt.hist(series1, label='series1', alpha=.8, edgecolor='red')
  
# plotting second histogram
plt.hist(series2, label='series2', alpha=0.7, edgecolor='yellow')
plt.legend()
  
# Showing the plot using plt.show()
plt.show()

输出:

示例 3:代表两个年龄组的直方图

Python

# importing libraries
import matplotlib.pyplot as plt
  
# giving two age groups data
age_g1 = [1, 3, 5, 10, 15, 17, 18, 16, 19, 21,
          23, 28, 30, 31, 33, 38, 32, 40, 45, 
          43, 49, 55, 53, 63, 66, 85, 80, 57, 
          75, 93, 95]
  
age_g2 = [6, 4, 15, 17, 19, 21, 28, 23, 31, 36,
          39, 32, 50, 56, 59, 74, 79, 34, 98, 97,
          95, 67, 69, 92, 45, 55, 77, 76, 85]
  
# plotting first histogram
plt.hist(age_g1, label='Age group1', alpha=.7, edgecolor='red')
  
# plotting second histogram
plt.hist(age_g2, label='Age group2', alpha=0.7, edgecolor='yellow')
plt.legend()
  
# Showing the plot using plt.show()
plt.show()

输出:

示例 4:

Python

# importing libraries
import matplotlib.pyplot as plt
  
# giving two age groups data
age_g1 = [1, 3, 5, 10, 15, 17, 18, 16, 19,
          21, 23, 28, 30, 31, 33, 38, 32, 
          40, 45, 43, 49, 55, 53, 63, 66, 
          85, 80, 57, 75, 93, 95]
  
age_g2 = [6, 4, 15, 17, 19, 21, 28, 23, 31, 
          36, 39, 32, 50, 56, 59, 74, 79, 34, 
          98, 97, 95, 67, 69, 92, 45, 55, 77,
          76, 85]
  
# plotting first histogram
plt.hist(age_g1, label='Age group1', bins=14, alpha=.7, edgecolor='red')
  
# plotting second histogram
plt.hist(age_g2, label="Age group2", bins=14, alpha=.7, edgecolor='yellow')
plt.legend()
  
# Showing the plot using plt.show()
plt.show()

输出:

示例 5 :从默认更改条形颜色

Python

# importing libraries
import matplotlib.pyplot as plt
  
# giving two age groups data
age_g1 = [1, 3, 5, 10, 15, 17, 18, 16, 19, 21,
          23, 28, 30, 31, 33, 38, 32, 40, 45, 
          43, 49, 55, 53, 63, 66, 85, 80, 57, 
          75, 93, 95]
  
age_g2 = [6, 4, 15, 17, 19, 21, 28, 23, 31, 36,
          39, 32, 50, 56, 59, 74, 79, 34, 98, 97,
          95, 67, 69, 92, 45, 55, 77, 76, 85]
  
# plotting first histogram
plt.hist(age_g1, label='Age group1', alpha=.7, color='red')
  
# plotting second histogram
plt.hist(age_g2, label="Age group2", alpha=.5,
         edgecolor='black', color='yellow')
plt.legend()
  
# Showing the plot using plt.show()
plt.show()

输出: