numpy和plt常见函数总结

1、linspace

#coding:utf-8
import numpy as np
#[0,1]之间默认划为50个点
a=np.linspace(0, 1)
#[0,1]之间划分为10个点,
b=np.linspace(0,1,10)
print a
print b
print len(a)   #50
print len(b)   #10

2、random(random.random/rand/randn/seed)

#coding:utf-8
import numpy as np
#随机生成的数是固定的,第一个、第二个...都是固定的,每次都一样
np.random.seed(0)
a=np.random.randn(4)
print a

3、mgrid、ogrid (返回多维结构)

import numpy as np
#返回一维结构(两参数,默认间隔1,三参数,默认间隔是第三个参数,注意区间[1,7))
#若第三个参数是nj,包含开始和结束两点,共返回n个点,均分
a=np.mgrid[1:7]    #[1,2,3,4,5,6]
print a
a=np.mgrid[1:7:5]  #[1,6]
print a
a=np.mgrid[1:7:5j] #[1, 2.5, 4, 5.5, 7]
print a
#返回二维结构
b=np.mgrid[0:10:2,1:10:2]
print b
b=np.mgrid[1:5,1:3]
print b
#mgrid和ogrid一维使用是一致的,二维有点区别
#两个参数:第一个表示列,第二个表示行
#mgrid只是把列和行分别进行了扩展,得到了两个矩阵
#ogrid保留两个向量
c=np.ogrid[0:10:2,1:10:2]
print c
c=np.ogrid[1:5,1:3]
print c
'''
[[[0 0 0 0 0]
  [2 2 2 2 2]
  [4 4 4 4 4]
  [6 6 6 6 6]
  [8 8 8 8 8]]

 [[1 3 5 7 9]
  [1 3 5 7 9]
  [1 3 5 7 9]
  [1 3 5 7 9]
  [1 3 5 7 9]]]
[[[1 1]
  [2 2]
  [3 3]
  [4 4]]

 [[1 2]
  [1 2]
  [1 2]
  [1 2]]]
[array([[0],
       [2],
       [4],
       [6],
       [8]]), array([[1, 3, 5, 7, 9]])]
[array([[1],
       [2],
       [3],
       [4]]), array([[1, 2]])]
'''

注意:区别mgrid和meshgrid的区别

'''
①传参形式不同
②返回结果不同(行列刚好相反)
④都是先扩展第一个参数
'''
a=np.array([1,2,3])
b=np.array([4,5])
print np.mgrid[1:3:3j,4:5:2j]
print np.meshgrid(b,a)         #参数a和b可以任何形式矩阵,然后会被默认reshape成向量形式
'''
[[[ 1.  1.]
  [ 2.  2.]
  [ 3.  3.]]

 [[ 4.  5.]
  [ 4.  5.]
  [ 4.  5.]]]
[array([[4, 5],
       [4, 5],
       [4, 5]]), array([[1, 1],
       [2, 2],
       [3, 3]])]
'''

4、c_、r_、column_stack、row_stack

import numpy as np
a=np.array([1,2,3]).reshape(1,3)
b=np.array([4,5]).reshape(1,2)
print np.column_stack((a,b))
print np.c_[a,b]
#两种方法进行列扩展和行扩展(保证矩阵是规范形式reshape)
a=np.array([[1,2,3],[4,5,6]])
b=np.array([7,8,9]).reshape(1,3)
print np.row_stack((a,b))
print np.r_[a,b]
'''
[[1 2 3 4 5]]

[[1 2 3]
 [4 5 6]
 [7 8 9]]
'''

5、title、xlabel、ylabel、plot、scatter、legend

①写入中文怎么处理

②一张图可以画多条曲线,怎么打标签(plot、scatter)

③怎么设置标签的区域和颜色(legend)

④画的是线图,还是散点(一个是散点,一个是把散点串起来)

import numpy as np
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
#引入字体,画图中可以写入中文
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14) 
x=np.array([1,2,3])
y=np.array([1,2,3])
plt.plot(x,y,label='hqh')
plt.xlabel(u"x轴", fontproperties=font)
plt.ylabel(u'y轴', fontproperties=font)
plt.title(u"y=x的图像", fontproperties=font)
#打标签,可以在plot中打,也可以在legend中打(前者标签长度可以很长,后者只能一位)
leg=plt.legend('x',loc="upper center")
#设置颜色
leg.get_frame().set_facecolor("#00FFCC")
plt.show()
z=np.array([0,1,2])
plt.scatter(x,y,c=z)
plt.xlabel(u"x轴", fontproperties=font)
plt.ylabel(u'y轴', fontproperties=font)
plt.title(u"散点", fontproperties=font)
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值