简单实现python画圆功能

yipeiwu_com5年前Python基础

本文实例为大家分享了python实现画圆功能的具体代码,供大家参考,具体内容如下

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.patches import Polygon 
import matplotlib.patches as mpatches 
 
fig = plt.figure(figsize = (16,8)) 
ax = fig.gca() 
ax.set_xlim(-5,18) 
ax.set_ylim(-5,8) 
alpha = np.linspace(-np.pi,np.pi,30)  
t = np.linspace(-np.pi,np.pi,100) 
def ppp(nc , na , c , sx , sy): 
 beta = alpha[na] 
 gam = alpha[na+1] 
 x = [] ; y = [] ; 
 r = 0.5*nc 
 x.append(r*np.cos(beta)) ; y.append(r*np.sin(beta)) ; 
 tt = np.linspace(beta,gam,10) 
 for i in range(len(tt)): 
  x.append(r*np.cos(tt[i])) 
  y.append(r*np.sin(tt[i])) 
 r-=0.5 
 x.append(r*np.cos(gam)) ; y.append(r*np.sin(gam)) 
 tt = np.linspace(gam,beta,10) 
 for i in range(len(tt)): 
  x.append(r*np.cos(tt[i])) ; y.append(r*np.sin(tt[i])) 
 x = np.array(x) ; y = np.array(y) 
 verts = list(zip(x+sx,y+sy)) 
 poly = Polygon(verts , facecolor=c , edgecolor=c) 
 ax.add_patch(poly) 
 
#ppp(2,0,'r',0,0) 
def get_coordiate(x , y): 
 beta = (alpha[y]+alpha[y+1])/2 
 pr = 0.5*(x+x-1)/2 
 sx = pr*np.cos(beta) ; sy = pr*np.sin(beta) 
 return sx,sy 
def pp_arrow(x,y): 
 print x , y 
 sx , sy = get_coordiate(x, y) 
 for i in range(-1,2): 
  for j in range(-1,2): 
   if i == 0 and j == 0: 
    continue 
   ex , ey = get_coordiate(x+i, y+j) 
   ax.arrow(sx,sy,ex-sx,ey-sy,head_width=0.05,head_length=0.1,fc='r',ec='r') 
    
def pxy(cx,cy): 
 for i in range(10): 
  plt.plot(0.5*i*np.cos(t)+cx,0.5*i*np.sin(t)+cy)  
 for i in range(len(alpha)): 
  plt.plot((0.5*np.cos(alpha)+cx,4.5*np.cos(alpha)+cx),(0.5*np.sin(alpha)+cy,4.5*np.sin(alpha)+cy)) 
 
cx = 13 ; cy =0 
pxy(0,0) 
pxy(13,0) 
 
mr = np.zeros((10,30)) 
mb = np.zeros((10,30)) 
 
for i in range(3,9): 
 for j in range(1,27): 
  ttt = np.random.random() 
  if ttt >= 0.98 and ttt <= 1: 
   ppp(i,j,'r',0,0) 
   mr[i][j] = 1 
   continue 
  if ttt < 0.8: 
   continue 
  else: 
   ppp(i,j,'b',0,0) 
   mb[i][j] = 1 
 
for i in range(10): 
 for j in range(30): 
  if mr[i][j] == 0: 
   continue 
  pp_arrow(i, j) 
  ma = 0 ;posx = 1 ; posy = 1 
  for k in range(-1,2): 
   for s in range(-1,2): 
    mb[i+k][j+s]=0 
    ttt = np.random.random() 
    if ttt > ma: 
     ma = ttt 
     posx = k ; posy = s 
  ppp(i+posx , j+posy , 'r' , 13 , 0) 
  ppp(i,j,'g',13,0) 
 
for i in range(10): 
 for j in range(30): 
  if mb[i][j] == 0: 
   continue 
  ppp(i , j , 'b' , 13 , 0) 
 
arrow = mpatches.Arrow(5, 0 
      ,3,0,width = 1,color = 'r') 
ax.add_patch(arrow) 
ax.text(6.2,0.5,'after one\nunit time',ha='center',va='center',color='y') 
 
xx = [4,4,4] 
yy = [7.5,6.5,5.5] 
c = ['b' , 'r' , 'g'] 
s = ['debris','satellite' , 'the location of satellite one unit time ago'] 
for i in range(3): 
 print xx[i] , yy[i] 
 rect = mpatches.Rectangle((xx[i],yy[i]),0.5,0.3,ec = c[i],fc=c[i] ) 
 ax.text(xx[i]+0.7,yy[i],s[i]) 
 ax.add_patch(rect) 
plt.axis('off') 
plt.show() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python将图片转base64,实现前端显示

python将图片转base64,实现前端显示

如下所示: # -*- coding: utf-8 -*- import base64 with open("C:\\Users\\user\\Desktop\\201705...

Python函数式编程指南(一):函数式编程概述

1. 函数式编程概述 1.1. 什么是函数式编程? 函数式编程使用一系列的函数解决问题。函数仅接受输入并产生输出,不包含任何能影响产生输出的内部状态。任何情况下,使用相同的参数调用函数始...

flask-socketio实现WebSocket的方法

【flask-socektio】 之前不知道在哪个场合下提到过如何从web后台向前台推送消息。听闻了反向ajax技术这种模式之后,大呼神奇,试了一下之后发现也确实可以用。不过,反向aj...

pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法

pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法

self.lineEdit.setEchoMode(QLineEdit.Password) 设置密码隐藏 self.lineEdit.setClearButtonEnabled(True...

Python库urllib与urllib2主要区别分析

作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版。今天看到老外写的一篇《Python: difference between urllib and...