Python中matplotlib如何绘制栈式直方图的示例


本文摘自php中文网,作者黄舟,侵删。

这篇文章主要介绍了Python基于matplotlib绘制栈式直方图的方法,涉及Python使用matplotlib进行图形绘制的相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python基于matplotlib绘制栈式直方图的方法。分享给大家供大家参考,具体如下:

平时我们只对一组数据做直方图统计,这样我们只要直接画直方图就可以了。

但有时候我们同时画多组数据的直方图(比如说我大一到大四跑大学城内环的用时的分布),大一到大四用不同颜色的直方图,显示在一张图上,这样会很直观。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

#!/usr/bin/env python

# -*- coding: utf-8 -*-

#http://www.jb51.net/article/100363.htm

# numpy array intorduction

#http://matplotlib.org/examples/statistics/histogram_demo_multihist.html

import numpy as np

import pylab as P

import matplotlib

d1=np.array([18.46,19.15,18.13 ,18.30 ,18.07 ,18.24 ,18.26 ,

     17.14 ,18.44 ,18.06 ,17.44 ,16.57 ,16.34 ,17.21 ])

d1=d1//1+(d1-d1//1)/0.6

d2=np.array([19.33 ,19.06 ,18.10 ,17.55 ,19.55 ,19.13 ,18.54 ,

     18.30 ,18.36 ,19.59 ,20.01 ,19.17 ,19.30 ,18.54 ,18.35 ,20.04 ])

d2=d2//1+(d2-d2//1)/0.6

d3=np.array([20.52 ,20.41 ,19.20 ,19.04 ,19.09 ,19.01 ,17.49 ,19.18 ,20.01 ,20.11 ])

d3=d3//1+(d3-d3//1)/0.6

d4=np.array([22.02 ,21.03,21.06 ,20.46 ,19.46 ,20.15 ,19.49 ,19.43 ,

       19.51 ,19.39 ,19.33 ,19.18 ,19.13 ,19.22 ,18.46 ,19.07 ,

       18.57 ,18.45 ,19.17 ,18.41 ,18.30 ])

d4=d4//1+(d4-d4//1)/0.6

x=([d1,d2,d3,d4])

P.figure()

#normed is False is good

n, bins, patches = P.hist(x, 12, [16.5, 22.5],normed=0, histtype='barstacked',

              color=['blue', 'green', 'red','yellow'],

             label=['   ', '   ', '   ','   '])

print type(x)

P.legend()#legend should be signed after set down the information

P.show()

以上图为例,很明显看到蓝色直方图(大一)跑得最快,黄色(大四)直方图跑得最慢。

以上就是Python中matplotlib如何绘制栈式直方图的示例的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python中正则表达式的知识汇总分享

Python如何使用正则表达式排除集合中字符的功能详解

Python调用xlsxwriter创建xlsx的方法

Python有这么强大吗

Python 编程开发 实用经验和技巧大放送

Python适合网页编程吗

Python里range是什么

Python下解压缩zip文件并删除文件的实例_Python

Python字典有set方法吗

Python中什么是元组和集合 ?元组和集合的介绍

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...