matplotlib——直方图

23 篇文章 0 订阅
5 篇文章 0 订阅

matplotlib——直方图

实验目的

了解hist函数的每个参数的含义

掌握使用matplotlib画直方图的方法

实验原理

直方图

函数原型:matplotlib.pyplot.hist(x, bins=None, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, hold=None, data=None, **kwargs)

  1. x : (n,) array or sequence of (n,) arrays(指定每个bin(箱子)分布的数据,对应x轴)
  2. bins : integer or array_like, optional,这个参数指定bin(箱子)的个数,也就是总共有几条条状图,默认为10;
  3. range : tuple or None, optional(设置显示的范围,范围之外的将被舍弃)
  4. normed :boolean,用于对数据进行正则化,当该参数的值为 Ture 时,图中显示的数据将会是正态正则化之后的结果
  5. weights : (n, ) array_like or None, optional(?)
  6. cumulative : boolean, optional就是每一列都把之前的加起来
  7. bottom : array_like, scalar, or None 条形图y坐标即每一个块的底部高度
  8. histtype : {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}, optional(选择展示bar的形状,默认为bar)
  9. align : {‘left’, ‘mid’, ‘right’}, optional(对齐方式)
  10. orientation : {‘horizontal’, ‘vertical’}, optional指的方向,分为水平与垂直两个方向。
  11. log : boolean, optional(log刻度)
  12. color : color or array_like of colors or None, optional(颜色设置)
  13. label : string or None, optional给图加刻度标签
  14. stacked : boolean, optional 作用就是当多个输入的时候,要不要把它们叠加起来
实验环境

Python 3.6.1

PyCharm

实验内容

使用matplotlib.pyplot中的hist函数绘制直方图。

实验步骤

1.打开Pycharm,选择Create New Project,

在这里插入图片描述

创建名为matplotlib6的项目。

在这里插入图片描述

2.打开matplotlib6项目,右键选择New=>Python File,

在这里插入图片描述

创建名为hist的Python文件。

在这里插入图片描述

3.打开hist.py文件,编写代码,用于绘制直方图。

导入包matplotlib的pyplot模块,用别名plt表示,导入包numpy,并用别名np表示。

  1. import numpy as np
  2. import matplotlib.pyplot as plt

4.创建一张图

  1. fig=plt.figure(1)

5.创建一个子图

  1. ax1=fig.add_subplot(111)

6.使用numpy包中的array函数绘制绘图所需的数据。

  1. np.random.seed(19680801)
  2. mu, sigma = 100, 15
  3. x = mu + sigma * np.random.randn(10000)

7.通过hist函数来绘制直方图的主体,并传入对数据进行正则化normed=1,条数bins=50,颜色facecolor=‘g’,透明度alpha=0.75等参数。

  1. n,bins,patches=ax1.hist(x,bins=50,normed=1,facecolor=‘g’,alpha=0.75)

8.添加一条最完美的mu=100,sigma=15正态曲线。

  1. y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
  2. np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
  3. ax1.plot(bins, y, ‘–’)

9.绘制x,y坐标轴标签、直方图内容标签,以及图形标题。

  1. ax1.set_xlabel(‘Smarts’)
  2. ax1.set_ylabel(‘Probability’)
  3. ax1.set_title(‘Histogram of IQ’)
  4. plt.text(60,0.025,r’ μ = 100 ,   σ = 15 \mu=100,\ \sigma=15 μ=100, σ=15’)

10.使用axis函数调整坐标范围,使用grid函数添加网格。

  1. ax1.axis([40,160,0,0.03])
  2. ax1.grid(True)
  3. plt.show()

11.完整代码:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. fig=plt.figure(1)
  4. ax1=fig.add_subplot(111)
  5. np.random.seed(19680801)
  6. mu, sigma = 100, 15
  7. x = mu + sigma * np.random.randn(10000)
  8. n,bins,patches=ax1.hist(x,bins=50,normed=1,facecolor=‘g’,alpha=0.75)
  9. y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
  10. np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
  11. ax1.plot(bins, y, ‘–’)
  12. ax1.set_xlabel(‘Smarts’)
  13. ax1.set_ylabel(‘Probability’)
  14. ax1.set_title(‘Histogram of IQ’)
  15. plt.text(60,0.025,r’ μ = 100 ,   σ = 15 \mu=100,\ \sigma=15 μ=100, σ=15’)
  16. ax1.axis([40,160,0,0.03])
  17. ax1.grid(True)
  18. plt.show()

12.代码编写完毕,在hist.py文件内,点击右键=》Run ‘hist’,执行hist.py文件。

在这里插入图片描述

13.在屏幕上打印出下面的图。

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余生羁绊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值