一、特点

数据必须是原始数据不能经过处理,数据连续型,显示一组或多组分布数据

histogram 直方图

normed 定额

二、核心

hist(x, bins=None, normed=None)
# x是需要统计的数据,类型:数组
# bins是组数, 组数 = (max(数组)- min(数组))//组距
# normed 默认为:频数分布直方图, 值为True为: 频率分布直方图

三、示例

1、频数直方图

from matplotlib import pyplot as plt
from matplotlib import font_manager a = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124,
101, 110,
116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111, 78, 132, 124, 113, 150, 110, 117, 86, 95, 144,
105, 126,
130, 126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136, 123, 117, 119, 105, 137, 123, 128, 125, 104,
109, 134,
125, 127, 105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114, 105, 115, 132, 145, 119, 121, 112, 139,
138, 109,
132, 134, 156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102, 123, 107, 143, 115, 136, 118, 139, 123, 112,
118, 125, 109,
119, 133, 112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135, 115, 146, 137, 116, 103, 144, 83, 123,
111, 110, 111,
100, 154, 136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141, 120, 117, 106, 149, 122, 122, 110, 118,
127, 121, 114,
125, 126, 114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92, 121, 112, 146, 97, 137, 105, 98, 117,
112, 81, 97,
139, 113, 134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110, 105, 129, 137, 112, 120, 113, 133, 112,
83, 94, 146,
133, 101, 131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111, 111, 133, 150, 120] my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msjh.ttc")
# 设置图行大小
plt.figure(figsize=(20, 8), dpi=80)
# 绘图
movie_width = 3
num_bins = (max(a) - min(a)) // movie_width
plt.hist(a, num_bins)
# 定制x轴刻度和label
_x = list(range(min(a), max(a) + 1))
plt.xticks(_x[::movie_width])
# 添加网格
plt.grid()
# 添加说明
plt.xlabel("电影时长 单位(分)", fontproperties=my_font)
plt.ylabel("数量", fontproperties=my_font)
plt.title("电影时长频数分布直方图", fontproperties=my_font)
# 展示图片
plt.show()

2、频率直方图

频数直方图->频率直方图, 只需要在绘图的时候添加 normed=True 即可

plt.hist(a, num_bins, normed=True)

注意:

MatplotlibDeprecationWarning:
The 'normed' kwarg was deprecated in Matplotlib 2.1 and will be removed in 3.1. Use 'density' instead.
plt.hist(a, num_bins, normed=True)

四、条形图->直方图

目的:解决处理后的数据不能使用直方图的问题

方案:

1.绘图时,width=1或height=1
2.设置x轴或y轴的刻度,注意设置刻度和绘图之间没有直接的关系

例子

from matplotlib import pyplot as plt
from matplotlib import font_manager
#
my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\msjh.ttc")
interval = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 60, 90]
width = [5, 5, 5, 5, 5, 5, 5, 5, 5, 15, 30, 60]
quantity = [836, 2737, 3723, 3926, 3596, 1438, 3273, 642, 824, 613, 215, 47]
# 显示中文 # 设置图行大小
plt.figure(figsize=(20, 8), dpi=80)
# 绘图
plt.bar(range(len(interval)), quantity, width=1) # 设置x轴刻度和label
_x = range(len(interval) + 1)
_x_ticks = [i - 0.5 for i in _x]
_x_label = interval + [150]
plt.xticks(_x_ticks, _x_label)
# 添加说明
plt.xlabel("间隔", fontproperties=my_font)
plt.ylabel("数量", fontproperties=my_font)
plt.title("人口普查", fontproperties=my_font)
# 添加网格
plt.grid()
# 展示图片
plt.show()

matplotlib 直方图的更多相关文章

  1. matplotlib 直方图绘制详解

    n, bins, patches = plt.hist(datasets, bins, normed=False, facecolor=None, alpha=None) 函数说明 用于绘制多个数据集 ...

  2. 【Python】matplotlib直方图纵轴显示百分比

    其实很简单,就是算了一下百分比权重,乘以了一个权重值 import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatt ...

  3. Matplotlib直方图绘制技巧

    情境引入 我们在做机器学习相关项目时,常常会分析数据集的样本分布,而这就需要用到直方图的绘制. 在Python中可以很容易地调用matplotlib.pyplot的hist函数来绘制直方图.不过,该函 ...

  4. matplotlib直方图

    import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib.font_manager import FontPro ...

  5. Matplotlib 饼图

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

  6. Matplotlib 多个图形

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

  7. Matplotlib 图形绘制

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

  8. Matplotlib 安装

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

  9. Matplotlib 入门

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

随机推荐

  1. 改善Azure App Service托管应用程序性能的几个技巧

    本文介绍了几个技巧,这些技巧可以改善Azure App Service托管应用程序的性能.其中一些技巧是你现在就可以进行的配置变更, 而其他技巧则可能需要对应用程序进行一些重新设计和重构. 开发者都希 ...

  2. TESTNG+JENKINS持续集成

    一.环境搭建 安装testNG插件到eclipse. -) 选择菜单 Help /Software updates / Find and Install. -) 点击add button然后在loca ...

  3. 2019-4-29-C#-从-short-转-byte-方法

    title author date CreateTime categories C# 从 short 转 byte 方法 lindexi 2019-4-29 12:8:39 +0800 2019-01 ...

  4. Linux 内核 kobject 初始化

    本书已经展示了许多数据类型, 带有简单的在编译或者运行时初始化机制. 一个 kobject 的初始化有些复杂, 特别当使用它的所有函数时. 不管一个 kobject 如何使用, 但是, 必须进行几个步 ...

  5. C语言动态内存

    动态分配内存的概述 在数组一章中,介绍过数组的长度是预先定义好的,在整个程序中固定不变,但是在实际的编程中,往往会发生这种情况,即所需内存空间取决于实际输入的数据,而无法预先确定.为了解决上述问题,c ...

  6. springmvc 参数校验/aop失效/@PathVariable 参数为空

    添加依赖 <!-- 参数校验 --> <dependency> <groupId>org.hibernate.validator</groupId> & ...

  7. 【Docker】安装MySQL彻底解决3306端口占用问题

    1.问题闪现: 初次up mysql报3306端口被占用 yunduo@YunDuo:~/Work/Learning/Docker/docker_compose$ docker-compose up ...

  8. .bash_profile 文件

    1,当 .bash_profile 文件输入有误的时候,所有命令行都会不好使 输入 export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin ...

  9. VS运行速度缓慢卡顿 2020年1月3日23:25:35

    Java中一个类文件对应一个类 C#中一个类文件中可以包含多个类 使用visual studio2017过程中,发现启动调试时,总是会很慢,结束调试也会很慢,在这里可以通过关闭掉IntelliTrac ...

  10. 电信NBIOT平台的CA证书上传-消息订阅回调地址检测503错误

    在NBIOT北向开发过程中,遇到消息订阅回调地址检测503错误,经过论坛查询与文档查阅一直都没有解决问题,大多人都说是RESTful地址格式问题,但其实不是.最终发现是我们在电信平台创建应用时,上传C ...