python ——matplotlib(2)

import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(3,3,1)              #绘制一个子图
n=128
X=np.random.normal(0,1,n)              #生成随机数
Y=np.random.normal(0,1,n)
T=np.arctan2(Y,X)                      #上色
#plt.axes([0.025,0.025,0.95,0.95])     #指定显示范围
ax.scatter(X,Y,s=75,c=T,alpha=.5)      #画散点图 size点的大小 color颜色
plt.xlim(-1.5,1.5),plt.xticks([])      #设定x,y范围,ticks设置为空
plt.ylim(-1.5,1.5),plt.yticks([])
plt.axis()
plt.title("scatter")
plt.xlabel("x")
plt.ylabel("y")






#柱状图


fig.add_subplot(332)
n=10
X=np.arange(n)                                              #构造数列
Y1=(1-X/float(n))*np.random.uniform(0.5,1.0,n)              #写出函数
Y2=(1-X/float(n))*np.random.uniform(0.5,1.0,n)
plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white')
plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white')
for x,y in zip(X,Y1):                                       #添加注释
    plt.text(x+0.4,y+0.05,'%2f'%y,ha='center',va='bottom')  #设置ha水平位置,va竖直位置
for x,y in zip(X,Y2):                                       #添加注释
    plt.text(x+0.4,-y-0.05,'%2f'%y, ha='center',va='top')




#Pie饼状图
fig.add_subplot(333)
n=20
Z=np.ones(n)                            #生成全一矩阵
Z[-1]*=2                                #最后一列设为2
plt.pie(Z,explode=Z*.05,colors=['%f'%(i/float(n)) for i in range(n)],
    labels=['%.2f'%(i/float(n)) for i in range(n)])
plt.gca().set_aspect('equal')           #让他生成一个正圆不是椭圆
plt.xticks([]),plt.yticks([])
#polor
fig.add_subplot(334,polar=True)
n=20
theta=np.arange(0.0,2*np.pi,2*np.pi/n)
radii=10*np.random.rand(n)
plt.plot(theta,radii)


#heatmap热度图
fig.add_subplot(335)
from matplotlib import cm
data=np.random.rand(3,3)          #定义3x3随机数
cmap=cm.Blues                     #
map=plt.imshow(data,interpolation='nearest',cmap=cmap,aspect='auto',vmin=0,vmax=1)






#3D图
from mpl_toolkits.mplot3d import Axes3D
ax=fig.add_subplot(336,projection="3d")
ax.scatter(1,1,3,s=100)




#hot map 热力图
fig.add_subplot(313)
def f(x,y):
    return(1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
n=256
x=np.linspace(-3,3,n)
y=np.linspace(-3,3,n)
X,Y=np.meshgrid(x,y)
plt.contour(X,Y,f(X,Y),8,alpha=.75,cmap=plt.cm.hot)
plt.savefig("./data/fig.png")




plt.show()



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值