用Python Matplotlib库绘制直方图程序

338 阅读5分钟

Matplotlib histogram - The Complete Guide

Python Matplotlib库协助可视化和研究数据,并在图形可视化的帮助下帮助更好地理解数据,可以使用matplotlib库来模拟。

Matplotlib直方图

matplotlib.pyplot.hist()matplotlib 的一个内置函数,它可以为给定的点创建一个直方图,并将该图作为输出显示。

语法

matplotlib.pyplot.hist(x, bins=None, range=None, density=False, 
                       weights=None, cumulative=False, bottom=None, histtype='bar',
                       align='mid', orientation='vertical', rwidth=None, log=False, 
                       color=None, label=None, stacked=False, *, data=None, **kwargs) 

参数

matplotlib.pyplot.hist() 函数有一个必要的参数:

    1. x:这是一个必要的参数。这个参数需要一个数组作为值。这个数组由点组成。
    2. bins: 这个参数以一个整数或整数序列作为值。这个参数是可选的。默认情况下,它被分配为10。
    3. range(范围):这是一个可选的参数。如果在范围参数中提供了下限值和上限值,那么小于下限值和大于上限值的值将不会被绘制在图表中。默认情况下,这个值提供的是下限值的最小值和上限值的最大值。
    4. 密度:这需要一个布尔值。如果这个值被传递为True,那么将返回概率密度函数。默认情况下,这个值被传递为False数据。一个带有标签数据的对象作为参数被传递。这是一个可选的参数。
    5. weights(权重):这是一个可选的参数。这个参数可以用一个数组的值来传递。这个数组的形状应该等于x数组的形状。
    6. 累加:这是一个可选的参数。这个参数将一个布尔值作为参数的值。如果传递的是True,那么每个bin的值都会与前一个bin的值相加,而总分则作为bin的值保留。
    7. 底部:这是一个可选的参数。这个参数在一个数组中取值。这个数组由bin的位置组成,bin将位于这些位置。
    8. histtype:这是一个可选的参数。这个参数使用字符串值。它表示需要绘制的直方图的类型。这些值可以是条形图、条形叠加图、阶梯图或阶梯填充图。
    9. align: 这个参数表示图表的对齐方式。它有左、右和中间等值。
    10. orientation(方向):这个值描述了图形的方向。它有垂直和水平这样的值。默认情况下,它是保持垂直的。
    11. rwidth:它是条形图的相对宽度。默认情况下,它被保持为无。
    12. 日志:它需要一个布尔值作为参数。如果是True,那么刻度将被设置为对数刻度。
    13. color: 图形的颜色在这个参数中被传递。颜色也可以在数组中传递。
    14. label(标签):这个参数字符串作为输入。如果在该阶段使用了多个数据集,第一个数据会得到该参数中传递的标签。其他数据得到图例中提供的值。
    15. stacked(堆叠):它需要一个布尔值作为这个参数的值。如果为真,那么多个值会被堆叠在一起。

返回值

matplotlib.pyplot.hist() 函数返回一个包含n个元素的数组。然后它返回 bins 数组。它还会返回patches数组。

使用 matplotlib.pyplot.hist 创建柱状图的程序

# Importing matplotlib.pyplot as plt.
import matplotlib.pyplot as plt

# Importing numpy as np
import numpy as np

# Random function is Seeded
np.random.seed(10**7)

# Random numbers are created and stored in x variable
x = np.random.randn(10000)

# Number of bins are assigned as 200
num_bins = 200

# Hist function is used
n, bins, patches = plt.hist(x, num_bins, density=2, color='blue')

# X-axis is labeled as X-Axis
plt.xlabel('X-Axis')

# y axis is labeled as Y-Axis
plt.ylabel('Y-Axis')

# Title is kept for the histogram
plt.title('Histogram Example')

# displaying the created graph using the show method
plt.show()

输出

Matplotlib histogram

在这个程序中,我们导入了matplotlib.pyplot来绘制柱状图。matplotlib库包含了所有用于绘制不同类型图形的函数。然后,我们导入numpy来创建X坐标。

然后,我们创建了一个随机数组,并将其存储在x中,然后我们将bin的数量分配为200。

然后,我们使用了 hist() 函数。首先,x数组作为必要的参数被传递,然后我们给了200个bin,然后我们给了密度为2,颜色为蓝色。这个函数返回三个数组。

使用matplotlib.pyplot.hist创建柱状图的程序

# Importing matplotlib.pyplot as plt.
import matplotlib.pyplot as plt

# Importing numpy as np
import numpy as np

# Random function is Seeded
np.random.seed(10**7)

# random numbers are created and stored in x variable
x =np.random.randn(5000)

# Number of bins are assigned as 200
num_bins = 10

# hist function is used
n, bins, patches = plt.hist(x, num_bins,density = True,color ='lime',orientation='horizontal')

# x axis is labeled as X-Axis
plt.xlabel('X-Axis')

# y axis is labeled as Y-Axis
plt.ylabel('Y-Axis')

# Title is kept for the histogram
plt.title('Histogram Example',fontweight ="bold")

# displaying the created graph using the show method
plt.show()

输出

Matplotlib histogram example

在这个程序中,我们导入了matplotlib.pyplot来绘制柱状图。matplotlib库包含了所有用于绘制不同类型图形的函数。

然后我们导入numpy来创建X坐标。然后,我们创建了一个随机数组,并将其存储在x中,然后,我们将bin的数量指定为10。

然后,我们调用hist() 函数。首先,x数组作为必要参数被传递,然后我们传递了10个bin的数量,然后我们传递了密度为True,然后我们传递了方向为水平,然后颜色为石灰。hist()函数返回三个数组。