Python中的 Matplotlib.axes.Axes.hist2d()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.hist2d()函数
matplotlib 库的 axes 模块中的Axes.hist2d()函数用于制作二维直方图。
Syntax: Axes.hist2d(self, x, y, bins=10, range=None, density=False, weights=None, cmin=None, cmax=None, *, data=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
- x, y : These parameter are the sequence of data.
- bins : This parameter is an optional parameter and it contains the integer or sequence or string.
- range : This parameter is an optional parameter and it the lower and upper range of the bins.
- density : This parameter is an optional parameter and it contains the boolean values.
- weights : This parameter is an optional parameter and it is an array of weights, of the same shape as x.
- cmin : This parameter has all bins that has count less than cmin will not be displayed.
- cmax : This parameter has all bins that has count more than cmax will not be displayed.
Returns: This returns the following:
- h :This returns the bi-dimensional histogram of samples x and y.
- xedges :This returns the bin edges along the x axis.
- yedges :This returns the bin edges along the y axis.
- image :This returns the QuadMesh.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.hist2d()函数:
示例 1:
# Implementation of matplotlib function
from matplotlib import colors
from matplotlib.ticker import PercentFormatter
import numpy as np
import matplotlib.pyplot as plt
N_points = 100000
x = np.random.randn(N_points)
y = .4 * x + np.random.randn(100000) + 5
fig, ax = plt.subplots()
ax.hist2d(x, y, bins = 100,
norm = colors.LogNorm(),
cmap ="Greens")
ax.set_title('matplotlib.axes.Axes.\
hist2d() Example')
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
from matplotlib import colors
import numpy as np
from numpy.random import multivariate_normal
import matplotlib.pyplot as plt
result = np.vstack([
multivariate_normal([10, 10],
[[3, 2], [2, 3]], size = 100000),
multivariate_normal([30, 20],
[[2, 3], [1, 3]], size = 1000)
])
fig, [axes, axes1] = plt.subplots(nrows = 2,
ncols = 1,
sharex = True)
axes.hist2d(result[:, 0], result[:, 1],
bins = 100, cmap ="GnBu",
norm = colors.LogNorm())
axes1.hist2d(result[:, 0], result[:, 1],
bins = 100, norm = colors.LogNorm())
axes.set_title('matplotlib.axes.Axes.\
hist2d() Example')
plt.show()
输出: