Python学习笔记--Numpy--Matplotlib基础

原文地址:https://github.com/lijin-THU/notes-python/blob/master/03-numpy/03.02-matplotlib-basics.ipynb



Matplotlib 基础

在使用Numpy之前,需要了解一些画图的基础。

Matplotlib是一个类似Matlab的工具包,主页地址为

http://matplotlib.org

导入 matplotlib 和 numpy

In [1]:
%pylab
 
           
Using matplotlib backend: Qt4Agg
Populating the interactive namespace from numpy and matplotlib

plot 二维图

 
          
plot ( y )
plot ( x , y )
plot ( x , y , format_string )

只给定 y 值,默认以下标为 x 轴:

In [2]:
 
           
% matplotlib inline
x = linspace ( 0 , 2 * pi , 50 )
plot ( sin ( x ))
Out[2]:
[<matplotlib.lines.Line2D at 0xa086fd0>]

给定 x 和 y 值:

In [3]:
plot(x, sin(x))
Out[3]:
[<matplotlib.lines.Line2D at 0xa241898>]

多条数据线:

In [4]:
 
           
plot ( x , sin ( x ),
x , sin ( 2 * x ))
Out[4]:
 
           
[<matplotlib.lines.Line2D at 0xa508b00>,
<matplotlib.lines.Line2D at 0xa508d30>]

使用字符串,给定线条参数:

In [5]:
plot(x, sin(x), 'r-^')
Out[5]:
[<matplotlib.lines.Line2D at 0xba6ea20>]

多线条:

In [9]:
 
           
plot ( x , sin ( x ), 'b-o' ,
x , sin ( 2 * x ), 'r-^' )
Out[9]:
 
           
[<matplotlib.lines.Line2D at 0xbcf1710>,
<matplotlib.lines.Line2D at 0xbcf1940>]

更多参数设置,请查阅帮助。事实上,字符串使用的格式与Matlab相同。


scatter 散点图

 
          
scatter ( x , y )
scatter ( x , y , size )
scatter ( x , y , size , color )

假设我们想画二维散点图:

In [10]:
plot(x, sin(x), 'bo')
Out[10]:
[<matplotlib.lines.Line2D at 0xbd6c0b8>]

可以使用 scatter 达到同样的效果:

In [11]:
scatter(x, sin(x))
Out[11]:
<matplotlib.collections.PathCollection at 0xbd996d8>

事实上,scatter函数与Matlab的用法相同,还可以指定它的大小,颜色等参数:

In [12]:
 
           
x = rand ( 200 )
y = rand ( 200 )
size = rand ( 200 ) * 30
color = rand ( 200 )
scatter ( x , y , size , color )
# 显示颜色条
colorbar ()
Out[12]:
<matplotlib.colorbar.Colorbar instance at 0x000000000C31F448>

多图

使用figure()命令产生新的图像:

In [13]:
 
           
t = linspace ( 0 , 2 * pi , 50 )
x = sin ( t )
y = cos ( t )
figure ()
plot ( x )
figure ()
plot ( y )
Out[13]:
[<matplotlib.lines.Line2D at 0xc680cf8>]

或者使用 subplot 在一幅图中画多幅子图:

subplot(row, column, index)
In [15]:
 
           
subplot ( 1 , 2 , 1 )
plot ( x )
subplot ( 1 , 2 , 2 )
plot ( y )
Out[15]:
[<matplotlib.lines.Line2D at 0xcd47518>]

向图中添加数据

默认多次 plot 会叠加:

In [16]:
 
           
plot ( x )
plot ( y )
Out[16]:
[<matplotlib.lines.Line2D at 0xcbcfd30>]

可以跟Matlab类似用 hold(False)关掉,这样新图会将原图覆盖:

In [17]:
 
           
plot ( x )
hold ( False )
plot ( y )
# 恢复原来设定
hold ( True )
Out[17]:
[<matplotlib.lines.Line2D at 0xcf4b9b0>]

标签

可以在 plot 中加入 label ,使用 legend 加上图例:

In [19]:
 
           
plot ( x , label = 'sin' )
plot ( y , label = 'cos' )
legend ()
Out[19]:
<matplotlib.legend.Legend at 0xd2089b0>

或者直接在 legend中加入:

In [21]:
 
           
plot ( x )
plot ( y )
legend ([ 'sin' , 'cos' ])
Out[21]:
<matplotlib.legend.Legend at 0xd51fb00>

坐标轴,标题,网格

可以设置坐标轴的标签和标题:

In [22]:
 
           
plot ( x , sin ( x ))
xlabel ( 'radians' )
# 可以设置字体大小
ylabel ( 'amplitude' , fontsize = 'large' )
title ( 'Sin(x)' )
Out[22]:
<matplotlib.text.Text at 0xd727dd8>

用 'grid()' 来显示网格:

In [23]:
 
           
plot ( x , sin ( x ))
xlabel ( 'radians' )
ylabel ( 'amplitude' , fontsize = 'large' )
title ( 'Sin(x)' )
grid ()

清除、关闭图像

清除已有的图像使用:

clf()

关闭当前图像:

close()

关闭所有图像:

close('all')

imshow 显示图片

灰度图片可以看成二维数组:

In [25]:
 
           
# 导入lena图片
from scipy.misc import lena
img = lena ()
img
Out[25]:
 
           
array([[162, 162, 162, ..., 170, 155, 128],
[162, 162, 162, ..., 170, 155, 128],
...,
[162, 162, 162, ..., 170, 155, 128], [ 43, 43, 50, ..., 104, 100, 98],
[ 44, 44, 55, ..., 104, 105, 108]])
[ 44, 44, 55, ..., 104, 105, 108],

我们可以用 imshow() 来显示图片数据:

In [26]:
 
           
imshow ( img ,
# 设置坐标范围
extent = [ - 25 , 25 , - 25 , 25 ],
# 设置colormap
cmap = cm . bone )
colorbar ()
Out[26]:
<matplotlib.colorbar.Colorbar instance at 0x000000000DECFD88>

更多参数和用法可以参阅帮助。

这里 cm 表示 colormap,可以看它的种类:

In [28]:
dir(cm)
Out[28]:
 
           

使用不同的 colormap 会有不同的显示效果。

In [29]:
imshow(img, cmap=cm.RdGy_r)
Out[29]:
<matplotlib.image.AxesImage at 0xe0883c8>

从脚本中运行

在脚本中使用 plot 时,通常图像是不会直接显示的,需要增加 show() 选项,只有在遇到 show() 命令之后,图像才会显示。

直方图

从高斯分布随机生成1000个点得到的直方图:

In [30]:
hist(randn(1000))
Out[30]:
 
           
(array([ 2., 7., 37., 119., 216., 270., 223., 82., 31., 13.]),
array([-3.65594649, -2.98847032, -2.32099415, -1.65351798, -0.98604181,
3.01881521]),
-0.31856564, 0.34891053, 1.0163867 , 1.68386287, 2.35133904,
<a list of 10 Patch objects>)

更多例子请参考下列网站:

http://matplotlib.org/gallery.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值