matplotlib入门教程--1(条形图, 直方图, 盒须图, 饼图)

作图首先要进行数据的输入,matplotlib包只提供作图相关功能,本身并没有数据读入、输出函数,针对各种试验或统计文本数据输入可以使用numpy提供的数据输入函数。

复制代码
# -*- coding: gbk -*-
"""
Created on Sun Jan 11 11:17:42 2015

@author: zhang
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = [u'SimHei']

#生成数据
dataOut = np.arange(24).reshape(4, 6)
print(dataOut)

#保存数据
np.savetxt('data.txt', dataOut, fmt = '%.1f')

#读取数据
data = np.loadtxt('data.txt')
print(data)
复制代码

plot 和 bar 函数

复制代码
# -*- coding: gbk -*-
"""
Created on Sun Jan 11 11:33:14 2015

@author: zhang
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = [u'SimHei']

data = np.random.randint(1, 11, 5)
x = np.arange(len(data))

plt.plot(x, data, color = 'r')
plt.bar(x, data, alpha = .5, color = 'g')

plt.show()
复制代码

结果图片

image

饼图

复制代码
# -*- coding: gbk -*-
"""
Created on Sun Jan 11 11:33:14 2015

@author: zhang
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = [u'SimHei']

data = np.random.randint(1, 11, 5)
x = np.arange(len(data))

#plt.plot(x, data, color = 'r')




# plt.bar(x, data, alpha = .5, color = 'g') plt.pie(data, explode = [0,0,.2 , 0, 0]) plt.show
复制代码

image

在实际工作中经常要对多组数据进行对比分析,这样需要在一个图表里表示出多个数据集。plot函数多数据集表示方法:

复制代码
# -*- coding: gbk -*-
"""
Created on Sun Jan 11 11:51:41 2015

@author: zhang
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = [u'SimHei']

data = np.random.randint(1, 5, (5, 2))
x = np.arange(len(data))

plt.plot(x, data[:, 0], '--', color = 'm')
plt.plot(x, data[:, 1], '-.', color = 'c')

plt.show()
复制代码

image

这里用到了matplotlib中defered rendering的概念,它是指在绘图过程中,只有你调用到plt.plot函数是其它的绘图指令才会起效。

也可以通过对条形图的定制实现数据对比,主要有这几种类型 multy bar chart;stack bar chart和back to back bar chart

复制代码
# -*- coding: gbk -*-
"""
Created on Sun Jan 11 12:03:57 2015

@author: zhang
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False

data = np.random.randint(1, 5, [3, 4])
index = np.arange(data.shape[1])
color_index = ['r', 'g', 'b']

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize = (5, 12))

for i in range(data.shape[0]):
    ax1.bar(index + i*.25 + .1, data[i], width = .25, color = color_index[i],\
    alpha = .5)

for i in range(data.shape[0]):
    ax2.bar(index + .25, data[i], width = .5, color = color_index[i],\
    bottom = np.sum(data[:i], axis = 0), alpha = .7)

ax3.barh(index, data[0], color = 'r', alpha = .5)
ax3.barh(index, -data[1], color = 'b', alpha = .5)

plt.show()
plt.savefig('complex_bar_chart')
复制代码

complex_bar_chart

统计中常用的两种图标是直方图和盒须图,matplotlib中有针对这两种图表的专门函数:hist和boxplot

复制代码
# -*- coding: gbk -*-
"""
Created on Sun Jan 11 12:29:34 2015

@author: zhang
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = [u'SimHei']

data = np.random.randn(100)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (8, 4))

ax1.hist(data)
ax2.boxplot(data)

plt.savefig('hist_boxplot')
plt.show()
复制代码

hist_boxplot

本文讲到的所有matplotlib命令都有非常丰富的定制参数,我会在后面文章中讲到,你也可以查看帮助文档学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值