名字长不好绕树林! 2019-04-11 21:10 采纳率: 50%
浏览 1786
已采纳

怎样用Python读入.txt里四个坐标点并绘制矩形?

现有.txt文件,包含若干个矩形,每一行为左上,右上,左下,右下坐标点,格式如:(10 10)(20 10)(10 20)(20 20),如何读入四个坐标显示,并绘制矩形框,请各位大神帮助,感激不尽!

  • 写回答

2条回答 默认 最新

  • 吃鸡王者 2019-04-12 15:33
    关注

    import matplotlib.pyplot as plt
    import re

    def line2points(line):
    xs=[]
    ys=[]
    line=line.strip()
    line=re.sub(r' +',',',line)
    points=eval(line)
    points=list(points)
    points.sort(key=lambda i:sum(i))
    points[-2:]=points[-1:-3:-1]
    points.append(points[0])
    for x,y in points:
    xs.append(x)
    ys.append(y)
    return xs,ys

    with open('datafilename.txt','r') as f:
    line=f.readline()
    x,y=line2points(line)
    plt.plot(x,y)
    line=lf.readline()
    plt.show()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料