如何使用 Matplotlib 绘制具有相等区域柱状图的直方图?


若要使用 matplotlib 绘制具有相等区域柱状图的直方图,可以执行以下步骤:

步骤

  • 设置图形大小,并调整子图之间和周围的填充。

  • 使用 numpy 创建随机数据点。

  • 使用可以使图块面积相等的 equal_area 方法绘制直方图。

  • 若要显示图形,请使用 show() 方法。

示例

Open Compiler
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True def equal_area(x, nbin): pow = 0.5 dx = np.diff(np.sort(x)) tmp = np.cumsum(dx ** pow) tmp = np.pad(tmp, (1, 0), 'constant') return np.interp(np.linspace(0, tmp.max(), nbin + 1), tmp, np.sort(x)) x = np.random.randn(1000) n, bins, patches = plt.hist(x, equal_area(x, 20), edgecolor='black') plt.show()

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

将生成以下输出:

更新日期: 02-02-2022

367 次浏览

开启你的 职业

通过完成课程获取认证

开始
广告