python不同颜色的边_如何在python中更改多边形的特定边缘的颜色?

There is a polygon and I am wondering how can I change color of particular edge of it like figure below.

import matplotlib.pyplot as plt

import numpy as np

## -----------------------Initialize Geometry-----------------------

pixels = 600

my_dpi = 100

coord = [[-150,-200],[300,-200],[300,0],[150,200],[-150,200]]

fig = plt.figure(figsize=( pixels/my_dpi, pixels/my_dpi), dpi=my_dpi)

plt.axes([0,0,1,1])

rectangle = plt.Rectangle((-300, -300), 600, 600, fc='k')

plt.gca().add_patch(rectangle)

polygon = plt.Polygon(coord,fc='w')

plt.gca().add_patch(polygon)

plt.axis('off')

plt.axis([-300,300,-300,300])

plt.savefig('figure1/5.jpg',dpi=my_dpi)

解决方案

The easiest way to do this would be to simply plot a line between the two relevant vertices of the polygon, i.e.

plt.plot([coords[0,0], coords[-1,0]], [coords[0,1], coords[-1,1]], color='r', lw=5)

Would give you

Although I recommend adding a border to the polygon with the same width as this line of the same color as the facecolor:

polygon = plt.Polygon(coord,fc='w',ec='w',lw=5)

As a way to make the red line appear flush. You can change which edge is colored you simply change the indices of coords[i,j] in plt.plot() and as long as the indices are adjacent (with wrapping - so last index and first index are adjacent) the line drawn will be an edge and not a diagonal.

Also note you can shorten the plotting command by using slices or a helper function but I have neglected this for the sake of being explicit.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值