人生的意义就是思考其意义分享 http://blog.sciencenet.cn/u/qianlivan 天体物理工作者,家乡云南昆明。

博文

编程随笔(一)画多个圈

已有 3185 次阅读 2017-4-18 21:50 |个人分类:总结|系统分类:科研笔记| 模块化

画多个圈的时候才深刻体会到程序模块化的好处。毕竟重复操作不需要重复写代码。能让计算机做的事就不要自己做,计算机比人快得多还比较准确。


import matplotlib.pyplot as plt
from pylab import *

def plotmultibeam(x,y,angle):
   x0=x
   y0=y
   angle=angle/180.0*np.pi
   
   fig=plt.figure()
   ax=fig.add_subplot(111)
   
   beam=2.95
   d=beam*2
   
   
   
   circle=[]
   circtemp=Circle(xy=(x0,y0),radius=beam/2,)
   circle.append(circtemp)
   plt.annotate('0,0',xy=(x0,y0))
   plt.annotate('1',xy=(x0,y0-0.5),color='red')
   for i in range(1,7,1):
       xtemp=x0+d*np.cos(np.pi/3.0*i+angle)
   ytemp=y0+d*np.sin(np.pi/3.0*i+angle)
   strtemp=str('%6.2f'%(d*np.cos(np.pi/3.0*i+angle)))+','+str('%6.2f'%(d*np.sin(np.pi/3.0*i+angle)))
       circtemp=Circle(xy=(xtemp,ytemp),radius=beam/2,)
       circle.append(circtemp)
   plt.annotate(strtemp,xy=(xtemp,ytemp))
       beamnum=i-1
   if(beamnum<2):
       beamnum=beamnum+6    
       plt.annotate(str(beamnum),xy=(xtemp,ytemp-0.5),color='red')
   
   d2=np.sqrt((d*np.cos(np.pi/3.0)+d)**2+(d*np.sin(np.pi/3.0))**2)
   for i in range(1,7,1):
       xtemp=x0+d2*np.cos(np.pi/3.0*(i-1)+np.pi/6.0+angle)
   ytemp=y0+d2*np.sin(np.pi/3.0*(i-1)+np.pi/6.0+angle)
   strtemp=str('%6.2f'%(d2*np.cos(np.pi/3.0*(i-1)+np.pi/6.0+angle)))+','+str('%6.2f'%(d2*np.sin(np.pi/3.0*(i-1)+np.pi/6.0+angle)))
       circtemp=Circle(xy=(xtemp,ytemp),radius=beam/2,)
       circle.append(circtemp)
   plt.annotate(strtemp,xy=(xtemp,ytemp))
       beamnum=i*2+1
   if(beamnum<9):
       beamnum=beamnum+12
       plt.annotate(str(beamnum),xy=(xtemp,ytemp-0.5),color='red')
   
   d3=d*2
   for i in range(1,7,1):
       xtemp=x0+d3*np.cos(np.pi/3.0*i+angle)
   ytemp=y0+d3*np.sin(np.pi/3.0*i+angle)
   strtemp=str('%6.2f'%(d3*np.cos(np.pi/3.0*i+angle)))+','+str('%6.2f'%(d3*np.sin(np.pi/3.0*i+angle)))
       circtemp=Circle(xy=(xtemp,ytemp),radius=beam/2,)
       circle.append(circtemp)
   plt.annotate(strtemp,xy=(xtemp,ytemp))
       beamnum=i*2+2
   if(beamnum<8):
       beamnum=beamnum+12
       plt.annotate(str(beamnum),xy=(xtemp,ytemp-0.5),color='red')
   
   for circ in circle:
       circ.set_alpha(1.0)
       circ.set_edgecolor([0.0,1.0,0.0])
       circ.set_fill(False)
       circ.set_linewidth(0.5)
   
       ax.add_patch(circ)
   
   x,y=0,0
   #ax.plot(x,y,'ro')
   ax.set_xlim(-10,10)
   ax.set_ylim(-10,10)
   xlabel('arcmin')
   ylabel('arcmin')
   
   plt.axis('equal')
   filename='multibeam_position'+str(angle*180.0/np.pi)+'.png'
   plt.savefig(filename)
   plt.show()


plotmultibeam(0.0,0.0,23.413)
#plotmultibeam(0.0,0.0,13.898)
#plotmultibeam(0.0,0.0,13.898)






https://blog.sciencenet.cn/blog-117333-1049718.html

上一篇:翻译随笔(四)常见科技术语英文缩写
下一篇:一本正经地胡说八道(二)自由
收藏 IP: 123.113.33.*| 热度|

1 魏焱明

该博文允许实名用户评论 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-20 11:52

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部