【问题标题】:Points (scatter) on a polygon do not show in matplotlibmatplotlib 中不显示多边形上的点(散点)
【发布时间】:2020-04-14 19:17:39
【问题描述】:

我想使用 matplotlib 绘制一些(简单的)多边形并在上面绘制一些点。多边形没问题,但散点不显示。有什么建议可以解决/纠正这个问题吗?

# imports
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

# polygons
gg = {'municipality 1': [(1, 1), (2, 5), (3, 3), (2, 0)],
      'municipality 2': [(3, 4), (4, 6), (5, 8), (6, 3)],
      'municipality 3': [(2, 0), (3, 3), (3, 4), (6, 3), (4, -3)]}

# points / places
gh = {'municipality 1': (4, 2), 'municipality 2': (2, 2), 'municipality 3': (5, 5)}

# set colors
gk = {'municipality 1': 'royalblue', 'municipality 2': 'tomato', 'municipality 3': 'springgreen'}

# figure
fig, ax = plt.subplots()

for gemeente in gg.keys():
    poly = Polygon(gg[gemeente], label=gemeente, facecolor=gk[gemeente], edgecolor='black', linewidth=1)
    ax.add_patch(poly)

for gemeente in gh.keys():
    ax.scatter(gh[gemeente][0], gh[gemeente][1], c='black', label=gemeente)

ax.legend()

plt.autoscale()
plt.show()

【问题讨论】:

    标签: python matplotlib geospatial polygon


    【解决方案1】:

    点在那里,但隐藏在多边形下。您可以使用zorder 参数来设置哪个图在上面:

    for gemeente in gg:
        poly = Polygon(gg[gemeente], label=gemeente, facecolor=gk[gemeente], edgecolor='black', linewidth=1, zorder=1)
        ax.add_patch(poly)
        ax.scatter(gh[gemeente][0], gh[gemeente][1], c='black', label=gemeente, zorder=2)
    

    【讨论】:

      猜你喜欢
      • 2014-09-05
      • 2023-01-18
      • 2016-12-29
      • 2013-07-07
      • 2016-11-25
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多