悄悄学Python——numpy

Matplotlib 基础

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

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

http://matplotlib.org

导入 matplotlibnumpy

%pylab

plot 二维图

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

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

%matplotlib inline
%pylab
x = linspace(0, 2 * pi, 50)
subplot(121)
plot(sin(x))

plot(x,sin(x),x,sin(2*x),x,sin(3*x))

图像:

当使用给定字符串,使用定点参数时。代码为:

plot(x,sin(x),'r--')

图像:

多条参数:

plot(x,sin(x),'r--',x,sin(2*x),'b--')

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

scatter 散点图

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

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

 

plot(x,sin(x),'bo')

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

scatter(x,sin(x))

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

x = rand(200)
y = rand(200)
size = rand(200) * 30
color = rand(200)
scatter(x, y, size, color)
# 显示颜色条
colorbar()

还可以产生多张图:

多图

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

t = linspace(0, 2*pi, 50)
x = sin(t)
y = cos(t)
figure()
plot(x)
figure()
plot(y)

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

subplot(row, column, index)

向图中添加数据

默认多次 plot 会叠加:

 

 
plot(x)

plot(y)
 

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

 

plot(x)
hold(False)
plot(y)
# 恢复原来设定
hold(True)

标签

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

plot(x, label='sin')
plot(y, label='cos')
legend()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值