matplotlib.axes.Axes.hist() 介绍
Matplotlib 直方图:matplotlib.axes.Axes.hist()
👏文章目录:
1、matplotlib.axes.Axes.hist() 介绍
-- 1.1、参数: x
-- 1.2、参数: bins
-- 1.3、参数: range
-- 1.4、参数: density
-- 1.5、参数: weights
-- 1.6、参数: cumulative
-- 1.7、参数: bottom
-- 1.8、参数: histtype
-- 1.9、参数: align
-- 1.10、参数: orientation
-- 1.11、参数: rwidth
-- 1.12、参数: log
-- 1.13、参数: color
-- 1.14、参数: label
-- 1.15、参数: stacked
-- 1.16、参数: data
-- 1.17、参数: **kwargs
1、matplotlib.axes.Axes.hist() 介绍
👏
Axes.hist()方法用于绘制直方图。
👏matplotlib.axes.Axes.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)
| 参数解释 |
|---|
1.x:要绘制直方图的数据。可以是一维数组、序列或其他可迭代对象。 |
2.bins:整数、序列或字符串。指定直方图的分箱数量或分箱边界。 |
3.range:元组(min, max)。指定数据的范围用于分箱。 |
4.density:布尔值。如果为 True,则直方图将显示概率密度而不是计数。 |
5.weights:与 x 形状相同的数组。用于为每个数据点指定权重。 |
6.cumulative:布尔值。如果为 True,则绘制累积直方图。 |
7.bottom:标量或数组。指定每个分箱底部的基线值。 |
8.histtype:字符串。指定直方图的类型,如 'bar'、'step'、'stepfilled' 等。 |
9.align:字符串。指定分箱的对齐方式,如 'left'、'mid'、'right'。 |
10.orientation:字符串。指定直方图的方向,如 'vertical'(垂直)或 'horizontal'(水平)。 |
11.rwidth:标量。指定分箱宽度相对于间隔的比例。 |
12.log:布尔值。如果为 True,则对数据进行对数变换后绘制直方图。 |
13.color:颜色参数。指定直方图的颜色。 |
14.label:字符串。为直方图设置标签,用于图例。 |
15.stacked:布尔值。如果为 True,则绘制堆叠直方图。 |
16.data:字典或 pandas.DataFrame。如果提供,则 x 可以是列名。 |
17.**kwargs:其他关键字参数,传递给 matplotlib.patches.Patch 的构造函数。 |
1.1、参数: x
👏
x是要绘制直方图的数据。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(x)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123101.png')
plt.show()

1.2、参数: bins
👏
bins指定直方图的分箱数量或分箱边界。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, bins=30
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123102.png')
plt.show()

1.3、参数: range
👏
range是一个元组(min, max),用于指定数据的范围以进行分箱。如果未提供,则使用数据的最小值和最大值。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, range=(20, 60)
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123103.png')
plt.show()

1.4、参数: density
👏如果
density=True,则直方图将显示概率密度而不是计数。这意味着直方图的总面积将为 1。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, density=True
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123104.png')
plt.show()

1.5、参数: weights
👏
weights是与x形状相同的数组,用于为每个数据点指定权重。
import numpy as np
x = [1, 2, 2, 3, 3, 3, 4, 4, 5]
weights = np.array([1, 2, 2, 3, 3, 3, 4, 4, 5])
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, weights=weights
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123105.png')
plt.show()

1.6、参数: cumulative
👏如果
cumulative=True,则绘制累积直方图。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, cumulative=True
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123106.png')
plt.show()

1.7、参数: bottom
👏
bottom是标量或数组,指定每个分箱底部的基线值。
如果 bottom = 2,那么绘制的直方图中每个分箱的底部都将从 y = 2 的位置开始,这样会使整个直方图在纵轴方向上整体上移或下移。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, bottom = 50
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123107.png')
plt.show()

1.8、参数: histtype
👏
histtype是字符串,指定直方图的类型,如'bar'(默认)、'step'、'stepfilled'等。
'bar' 是独立的矩形条,而 'stepfilled' 是填充的阶梯形状。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, histtype='step'
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123108.png')
plt.show()

1.9、参数: align
👏
align是字符串,指定分箱的对齐方式,如'left'、'mid'(默认)、'right'。
-
'left':-
分箱的左边缘与对应的数值对齐。
-
这意味着如果一个分箱的范围是 [a, b),那么该分箱的左边缘会与数值
a对齐。
-
-
'mid':-
默认值。
-
分箱的中间位置与对应的数值对齐。
-
例如对于分箱 [a, b),中间位置
(a + b) / 2会与相应的数值对齐。
-
-
'right':-
分箱的右边缘与对应的数值对齐。
-
对于分箱 [a, b),右边缘
b会与对应数值对齐。
-
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, align='right'
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123109.png')
plt.show()

1.10、参数: orientation
👏
orientation是字符串,指定直方图的方向,如'vertical'(默认)或'horizontal'。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, orientation='horizontal'
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123110.png')
plt.show()

1.11、参数: rwidth
👏
rwidth是标量,指定分箱宽度相对于间隔的比例。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, rwidth=0.9
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123111.png')
plt.show()

1.12、参数: log
👏如果
log=True,则对数据进行对数变换后绘制直方图。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, rwidth=0.9
, log=True
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123112.png')
plt.show()

1.13、参数: color
👏
color是颜色参数,用于指定直方图的颜色。可以是单一颜色字符串,也可以是颜色列表以绘制多组数据的不同颜色直方图。
x = data['Age']
import matplotlib.pyplot as plt
# 创建一个绘图窗体,并设置大小为 (3.5, 3)
fig, ax = plt.subplots(figsize=(3.5, 3))
ax.hist(
x
, rwidth=0.9
, color='orange'
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123113.png')
plt.show()

1.14、参数: label
👏
label是字符串,为直方图设置标签,用于在绘制图例时标识不同的直方图。
import matplotlib.pyplot as plt
data1 = [1, 2, 2, 3, 3, 3, 4, 4, 5]
data2 = [2, 3, 3, 4, 4, 4, 5, 5, 6]
ax = plt.gca()
ax.hist(data1, label='Data 1')
ax.hist(data2, label='Data 2')
ax.legend()
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123114.png')
plt.show()

1.15、参数: stacked
👏如果
stacked=True,则绘制堆叠直方图,即将多组数据的直方图堆叠在一起显示。
import matplotlib.pyplot as plt
data1 = [1, 2, 2, 3, 3, 3, 4, 4, 5]
data2 = [2, 3, 3, 4, 4, 4, 5, 5, 6]
ax = plt.gca()
ax.hist([data1, data2], stacked=True)
plt.show()

1.16、参数: data
👏
data可以是字典或pandas.DataFrame。如果提供,则x可以是列名,从data中提取数据进行直方图绘制。
import matplotlib.pyplot as plt
ax = plt.gca()
ax.hist(data['Age'])
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123116.png')
plt.show()

1.17、参数: **kwargs
👏
**kwargs是其他关键字参数,可传递给matplotlib.patches.Patch的构造函数,用于进一步自定义直方图的外观属性,如透明度、边框颜色等。
import matplotlib.pyplot as plt
ax = plt.gca()
ax.hist(
data['Age']
, color='Coral'
, rwidth=0.9
, alpha=0.5 # 透明度
, edgecolor='gray' # 条形边框颜色
, linewidth=0.65 # 条形边框宽度
)
# 保存图形为 PNG 格式
plt.savefig('matplotlib_24123117.png')
plt.show()

👏总结:
Axes.hist()方法用于绘制直方图。更多的 matplotlib 可以查看
Python 端到端的机器学习AI入门:详细介绍机器学习建模过程,步骤细节;以及人工智能的分阶段学习线路图。 🚀 点击查看 |
统计学习\机器学习\深度学习算法介绍有关统计学习,机器学习,深度学习的算法。 🚀 点击查看 |
SQL + Pandas 练习题SQL 练习题目,使用 Pandas 库实现,使用 Sqlalchemy 库查看 SQL 代码血缘关系。 🚀 点击查看 |
Python 数据可视化介绍了有关 Matplotlib,Seaborn,Plotly 几个 Python 绘图库的简单使用。 🚀 点击查看 |
更多推荐



所有评论(0)