我尝试填充多边形外部的区域,类似于本主题中解释的情况:Fill OUTSIDE of polygon | Mask array where indicies are beyond a circular boundary?。不幸的是,我的情况出了问题。我可以执行一些基本多边形的填充,只有很少的角落但是当我尝试为我的目标图形执行此操作时,整个图形正在填充,而不仅仅是它的外部。填补补丁是否有任何限制,或者我做错了什么? 以下是我正在使用的示例代码。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.patches as mpatches
xg, yg = np.mgrid[ax.min():ax.max():100j, ay.min():ay.max():100j]
rbf1 = Rbf(ax, ay, az, function='linear')
za = rbf1(xg, yg)
plt.contourf(xg, yg, za)
#some exemplary working polygon
#poly_verts = [[0.0432, -0.04234], [0.06542, -0.032345], [0.055423, -0.022345], [0.0355423, -0.01], [0.035234523, -0.0323452354], [0.0432, -0.04234]]
#data that is not working
poly_verts = ins_path
ax_s = plt.gca()
xlim = ax_s.get_xlim()
ylim = ax_s.get_ylim()
bound_verts = [(xlim[0], ylim[0]), (xlim[0], ylim[1]), (xlim[1], ylim[1]), (xlim[1], ylim[0]), (xlim[0], ylim[0])]
bound_codes = [mpath.Path.MOVETO] + (len(bound_verts) - 1) * [mpath.Path.LINETO]
poly_codes = [mpath.Path.MOVETO] + (len(poly_verts) - 1) * [mpath.Path.LINETO]
path = mpath.Path(bound_verts + poly_verts, bound_codes + poly_codes)
patch = mpatches.PathPatch(path, facecolor='white')
patch = ax_s.add_patch(patch)
ax_s.set_xlim(xlim)
ax_s.set_ylim(ylim)
plt.show()
以下是我使用的一些示例性数据:exemplary data
提前感谢任何建议。
答案 0 :(得分:0)
您好,我花了一些时间,但答案来了我:)。我不确定它是不是违反规则,但我认为答案可能对某人有用,所以我决定分享它。 问题在于我的路径方向,如果你这样反转:
poly_verts = ins_path[::-1]
代码非常完美。似乎边界的方向必须相同,否则在整个区域进行填充(可能是路径混合)。