numpy的导入方法有以下几种:
import numpy
import numpy as np(常用)
from numpy import *
from numpy import array,sin
from numpy import *
import matplotlib.pyplot as plt
from numpy.random import rand
#
# #数组上的数学操作
# a=[1,2,3,4]
# a=array(a)
# print(a)
# #array 数组支持每个元素加 1 这样的操作:
# print(a+1)
# #与另一个 array 相加,得到对应元素相加的结果:
# b=array([2,3,4,5])
# print(a+b)
# #对元素相乘
# print(a*b)
# #对应元素乘方
# print(a**b)
# ####################
# #提取数组中的元素
# #提取第1个元素
# print(a[0])
# #提取前两个元素
# print(a[:2])
# #最后两个元素:
# print(a[-2:])
# #将它们相加:
# print(a[:2]+a[-2:])
# ###########################
# #修改数组形状
# #查看 array 的形状:
# print(a.shape)
# #修改 array 的形状:
# a.shape=2,2
# print(a)
# ######################
# #多维数组
# #a 现在变成了一个二维的数组,可以进行加法:
# print(a+a)
# #乘法仍然是对应元素的乘积,并不是按照矩阵乘法来计算:
# print(a*a)
# ######################
# #画图
# #linspace 用来生成一组等间隔的数据:
# a=linspace(0,2*pi,21)
# print(a)
# #三角函数:
# b=sin(a)
# print(b)
# #画出图像:
# # plt.plot(a,b)
# # plt.show()
# #####################
# #从数组中选择元素
# print(b>=0)
# mask=b>=0
# # #画出所有对应的非负值对应的点:
# # plt.plot(a[mask], b[mask], 'ro')
# # plt.show()
# ###########################matplotlib基础
# #plot二维图
# x=linspace(0,2*pi,50)
# # plt.plot(sin(x))
# # plt.show()
# # plt.plot(x,sin(x))
# # plt.show()
# # plt.plot(x,sin(x),x,sin(2*x))
# # plt.show()
# # plt.plot(x,sin(x),'r-')
# # plt.show()
# # plt.plot(x,sin(x),'b-o',x,sin(2*x),'r-')
# # plt.show()
# #scatter 散点图
# # plt.plot(x,sin(x),'bo')
# # plt.show()
# # plt.scatter(x,sin(x))#scatter函数与Matlab的用法相同,还可以指定它的大小,颜色等参数:
# # plt.show()
# x=rand(200)
# print(x)
# y=rand(200)
# size=rand(200)*30
# color=rand(200)
# # plt.scatter(x,y,size,color)
# # plt.colorbar()
# # plt.show()
# #多图
# t=linspace(0,2*pi,50)
# x=sin(t)
# y=cos(t)
# # plt.figure()
# # plt.plot(x)
# # plt.figure()
# # plt.plot(y)
# # plt.show()
# #使用 subplot 在一幅图中画多幅子图:subplot(row,column,index)
# # plt.subplot(1,2,1)
# # plt.plot(x)
# # plt.subplot(1,2,2)
# # plt.plot(y)
# # plt.show()
# #向图中添加数据
# # plt.plot(x)
# # plt.plot(y)
# # plt.show()
# #可以跟Matlab类似用 hold(False)关掉,这样新图会将原图覆盖:
# # plt.plot(x)
# # plt.hold(False)
# # plt.plot(y)
# # plt.hold(True)
# #标签
# # plt.plot(x,label='sin')
# # plt.plot(y,label='cos')
# # plt.legend()
# # plt.show()
# #
# # plt.plot(x)
# # plt.plot(y)
# # plt.legend(['sin','cos'])
# # plt.show()
# #坐标轴,标题,网格
# plt.plot(x,sin(x))
# plt.xlabel('radians')
# plt.ylabel('amplitude',fontsize='large')
# plt.title('Sin(x)')
# plt.show()
# #网格
# plt.plot(x,sin(x))
# plt.xlabel('radians')
# plt.ylabel('amplitude',fontsize='large')
# plt.title('Sin(x)')
# plt.grid()
# plt.show()
# #
# # 清除、关闭图像
# # 清除已有的图像使用:
# plt.clf()
# #关闭当前图像:
# plt.close()
# # 关闭所有图像:
# plt.close('all')
#显示图片数据
from scipy.misc import face,lena,ascent
img=face()
img1=ascent()
# print(img)
# print(img1)
plt.imshow(img,extent=[-25,25,-25,25],cmap=plt.cm.bone)
plt.colorbar()
plt.show()
# 这里 cm 表示 colormap,可以看它的种类:
# dir(cm)
plt.imshow(img,cmap=plt.cm.RdGy_r)
plt.show()
#直方图
plt.hist(rand(
1000))
plt.show()