4.6Python数据处理篇之Matplotlib系列(六)---plt.hist()与plt.hist2d()直方图

目录

前言

今天我们学习的是直方图,导入的函数是:

plt.hist(x=x, bins=10) 与plt.hist2D(x=x, y=y)

(一)直方图

(1)说明:

pyplot.``hist(x, bins=None, density=None,……kwargs*)

常见的参数属性

具体参考:官网说明文档

属性 说明 类型
x 数据 数值类型
bins 条形数 int
color 颜色 "r","g","y","c"
density 是否以密度的形式显示 bool
range x轴的范围 数值元组(起,终)
bottom y轴的起始位置 数值类型
histtype 线条的类型 "bar":方形,"barstacked":柱形,
"step":"未填充线条"
"stepfilled":"填充线条"
align 对齐方式 "left":左,"mid":中间,"right":右
orientation orientation "horizontal":水平,"vertical":垂直
log 单位是否以科学计术法 bool

(2)源代码:

# 导入模块
import matplotlib.pyplot as plt
import numpy as np

# 数据
mu = 100  # 均值
sigma = 20  # 方差
# 2000个数据
x = mu + sigma*np.random.randn(2000)


# 画图 bins:条形的个数, normed:是否标准化
plt.hist(x=x, bins=10)


# 展示
plt.show()

(3)输出效果:

默认:y轴是个数

01.png

改:plt.hist(x=x, bins=10, density=True)

y轴是频率

02.png

(二)双直方图

(1)说明:

pyplot.``hist2d(x, y, bins=10, **kwargs)

常见的参数属性

具体参考:官网说明文档

x x坐标
y y坐标
bins 横竖分为几条

(2)源代码:

# 导入模块
import matplotlib.pyplot as plt
import numpy as np

# 数据
x = np.random.randn(1000)+2
y = np.random.randn(1000)+3

# 画图
plt.hist2d(x=x, y=y, bins=30)

# 展示
plt.show()

(3)输出效果:

03.png

作者:Mark

日期:2019/02/13 周三

猜你喜欢

转载自www.cnblogs.com/zyg123/p/10504645.html