matplotlib 如何更改面片的不透明度/透明度(alpha),多边形边线

svdrlsy4  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(155)

我想改变这些多边形的边线的alpha值,而不依赖于matplotlib中的面片Polygon。我想让边线的alpha值大于多边形的面片。面片要超出边线,如何将面片完美地贴合在边线上,这样它就不会超出多边形。
代码如下:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Polygon
from scipy.spatial import ConvexHull
hull = ConvexHull(points)
# plt.plot(points[:,0], points[:,1], 'o')
cent = np.mean(points, 0)
pts = []
for pt in points[hull.simplices]:
        pts.append(pt[0].tolist())
        pts.append(pt[1].tolist())

pts.sort(key=lambda p: np.arctan2(p[1] - cent[1],
                        p[0] - cent[0]))
pts = pts[0::2]  # Deleting duplicates
pts.insert(len(pts), pts[0])
k = 0.4
poly = Polygon(k*(np.array(pts)- cent) + cent,fill=True,closed=True,
        facecolor=color, alpha=0.2,edgecolor=color,linewidth=7)
plt.gca().add_patch(poly)
plt.show()

输出在这里:

dddzy1tm

dddzy1tm1#

我想你可能需要把它们分开画-基本上是:

poly = Polygon(k*(np.array(pts)- cent) + cent,fill=True,closed=True,
        facecolor=color, alpha=0.2)
polyEdge = Polygon(k*(np.array(pts)- cent) + cent,fill=False,closed=True,
        alpha=0.8,edgecolor=color,linewidth=7)

给出这种外观:x1c 0d1x

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Polygon
from scipy.spatial import ConvexHull
points = np.array([(.30,.60), (.50,.40), (0,.30),(.15,.25),(.20,0),(.50,.10),(.55,.20),(.70,.30)])
hull = ConvexHull(points)
# plt.plot(points[:,0], points[:,1], 'o')
cent = np.mean(points, 0)
pts = []
for pt in points[hull.simplices]:
        pts.append(pt[0].tolist())
        pts.append(pt[1].tolist())

color='red'
pts.sort(key=lambda p: np.arctan2(p[1] - cent[1],
                        p[0] - cent[0]))
pts = pts[0::2]  # Deleting duplicates
pts.insert(len(pts), pts[0])
k = 0.4
poly = Polygon(k*(np.array(pts)- cent) + cent,fill=True,closed=True,
        facecolor=color, alpha=0.2)
polyEdge = Polygon(k*(np.array(pts)- cent) + cent,fill=False,closed=True,
        alpha=0.8,edgecolor=color,linewidth=7)

plt.gca().add_patch(poly)
plt.gca().add_patch(polyEdge)
plt.show()

相关问题