python绘制多边形_如何使用matplotlib PATH绘制多边形

I have a problem when using python's matplotlib PATH modules

I want to draw a close poly like this:

but I don't know exactly the sequence of the points to be connected and it turned out the result images can't meet my needs. How can I draw a polygon correctly without determining the sequence by myself but by the code?

here is my code:

import matplotlib

import matplotlib.pyplot as plt

import pandas

from matplotlib.path import Path

import matplotlib.patches as patches

#read data

info = pandas.read_csv('/Users/james/Desktop/nba.csv')

info.columns = ['number', 'team_id', 'player_id', 'x_loc', 'y_loc',

'radius', 'moment', 'game_clock', 'shot_clock', 'player_name',

'player_jersey']

#first_team_info

x_1 = info.x_loc[1:6]

y_1 = info.y_loc[1:6]

matrix= [x_1,y_1]

z_1 = list(zip(*matrix))

z_1.append(z_1[4])

n_1 = info.player_jersey[1:6]

verts = z_1

codes = [Path.MOVETO,

Path.LINETO,

Path.LINETO,

Path.LINETO,

Path.LINETO,

Path.CLOSEPOLY,

]

path = Path(verts, codes)

fig = plt.figure()

ax = fig.add_subplot(111)

patch = patches.PathPatch(path, facecolor='orange', lw=2)

ax.add_patch(patch)

ax.set_xlim(0, 100)

ax.set_ylim(0, 55)

plt.show()

and I got this:

解决方案

Matplotlib plots the points of a path in order they are given to patch.

This can lead to undesired results, if there is no control over the order, like in the case from the question.

So the solution may be to

(A) use a hull. Scipy provides scipy.spatial.ConvexHull to calculate the circonference of the points, which is automatically in the correct order. This gives good results in many cases, see first row, but may fail in other cases, because points inside the hull are ignored.

(B) sort the points, e.g. counter clockwise around a certain point in the middle. In the example below I take the mean of all points for that. The sorting can be imagined like a radar scanner, points are sorted by their angle to the x axis. This solves e.g. the problem of the hull in the second row, but may of course also fail in more complicated shapes.

import numpy as np

import matplotlib.pyplot as plt

from scipy.spatial import ConvexHull

p = [(1,1), (2,1.6), (0.8,2.7), (1.7,3.2)]

p2 = [(0.7,1.3),(2,0.9),(1.4,1.5),(1.9,3.1),(0.6,2.5),(1.4,2.3)]

def convexhull(p):

p = np.array(p)

hull = ConvexHull(p)

return p[hull.vertices,:]

def ccw_sort(p):

p = np.array(p)

mean = np.mean(p,axis=0)

d = p-mean

s = np.arctan2(d[:,0], d[:,1])

return p[np.argsort(s),:]

fig, axes = plt.subplots(ncols=3, nrows=2, sharex=True, sharey=True)

axes[0,0].set_title("original")

poly = plt.Polygon(p, ec="k")

axes[0,0].add_patch(poly)

poly2 = plt.Polygon(p2, ec="k")

axes[1,0].add_patch(poly2)

axes[0,1].set_title("convex hull")

poly = plt.Polygon(convexhull(p), ec="k")

axes[0,1].add_patch(poly)

poly2 = plt.Polygon(convexhull(p2), ec="k")

axes[1,1].add_patch(poly2)

axes[0,2].set_title("ccw sort")

poly = plt.Polygon(ccw_sort(p), ec="k")

axes[0,2].add_patch(poly)

poly2 = plt.Polygon(ccw_sort(p2), ec="k")

axes[1,2].add_patch(poly2)

for ax in axes[0,:]:

x,y = zip(*p)

ax.scatter(x,y, color="k", alpha=0.6, zorder=3)

for ax in axes[1,:]:

x,y = zip(*p2)

ax.scatter(x,y, color="k", alpha=0.6, zorder=3)

axes[0,0].margins(0.1)

axes[0,0].relim()

axes[0,0].autoscale_view()

plt.show()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值