当前位置:首页 >> 脚本专栏

python绘制封闭多边形教程

数据格式:(polygon.txt) 里面含有2个多边形,一行是一个点

0.085, 0.834,
0.024, 0.744,
0, 0.63,
0.024, 0.516,
0.085, 0.427,
0.5, 0.02,
0.675, 0.191,
0.795, 0.071,
0.815, 0.052,
0.835, 0.032,
0.84, 0.026,
0.844, 0.022,
0.856, 0.012,
0.871, 0.005,
0.886, 0.001,
0.903, 0,

0.823, 0.061,
0.823, 0.062,
0.826, 0.061,

import pylab as py
import time
from pylab import *
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
def readdata(ls):
  l = ls.split(',')
  if len(l)>2:
    return [l[0],l[1]]
  else:
    return None

if "__main__"==__name__:
  print(9)
  file_obj=open('fanmaker.txt')
  k=0
  line=file_obj.readline()
  polygon =[]
  while line:
      k+=1
      px=[]
      py=[] 
      while line:
        row = readdata(line)
        print (line)
        px.append(row[0])
        py.append(row[1])
        line=file_obj.readline()
        if line=='\n':
          ps=[]
          px.append(px[0])
          py.append(py[0])
          ps.append(px)
          ps.append(py)
          polygon.append(ps)
          line=file_obj.readline()
          break
  x=np.array(polygon[0][0])
  y=np.array(polygon[0][1])
##  plt.fill(x,y,'b')
  plt.plot(x,y,'b-', linewidth=2)
  for i in range(len(polygon)-1):
    x=np.array(polygon[i+1][0])
    y=np.array(polygon[i+1][1])
##    plt.fill(x,y,'b')
    plt.plot(x,y,'r-', linewidth=2)
    plt.hold(True)
##  plt.show()

  x1=[-0.5 ,1.5]
  x2=[-0.5,-0.5]
  x3=[1.5 ,-0.5]
  x4=[1.5,1.5]
  plt.plot(x1,x2,'-')
  plt.plot(x2,x3,'-')
  plt.plot(x3,x4,'-')
  plt.plot(x4,x1,'-')
  plt.axis([-0.5,1.5 ,-0.5,1.5])
  plt.show()
  #   plt.axis('tight')
  file_obj.close()

python绘制封闭多边形教程

以上这篇python绘制封闭多边形教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。