简体   繁体   English

如何在matplotlib中设置多边形补丁的边缘颜色

[英]How to set the edge color of polygon patch in matplotlib

I managed to render a polygon patch onto a matplotlib canvas.我设法将多边形补丁渲染到 matplotlib 画布上。

In the code below node_coods is a Nx2 numpy array containing a vertices of polygon.在下面的代码中, node_coods是一个包含多边形顶点的 Nx2 numpy 数组。

from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

polygon = Polygon(node_coods, closed=True, edgecolor='r')
patches = [polygon]
p = PatchCollection(patches, cmap=mpl.cm.jet, alpha=0.4)
ax.add_collection(p)

At present my plot looks like this, notice it does not have edge color, inspite of passing the argument as given in manual .目前我的情节看起来像这样,注意它没有边缘颜色,尽管传递了手册中给出的参数。 How do I set the edge color of the polygon patch?如何设置多边形补丁的边缘颜色? I would like to set it to red, (but I'd like to have it tunable to any RGB value later)我想将其设置为红色,(但我想稍后将其调整为任何 RGB 值)

在此处输入图片说明

Since you do既然你这样做

p = PatchCollection(patches,  alpha=0.4)

per the documentation, edgecolors is default to None , which got override by matplotlib.rcParams default settings, which is transparent.根据文档, edgecolors默认为None ,它被matplotlib.rcParams默认设置覆盖,这是透明的。 So either do:所以要么做:

p = PatchCollection(patches, edgecolor='r', alpha=0.4)
ax.add_collection(p)

which gives这使

在此处输入图片说明

Or just use add_patch :或者只是使用add_patch

polygon = Polygon(node_coods, closed=True, edgecolor='r')
# patches = [polygon]
# p = PatchCollection(patches,  alpha=0.4)

fig, ax = plt.subplots()
ax.add_patch(polygon)

which gives:这使:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM