提取由matplotlib补丁括起来的坐标. [英] Extract coordinates enclosed by a matplotlib patch.

查看:60
本文介绍了提取由matplotlib补丁括起来的坐标.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 matplotlib.patches.ellipse 创建了一个椭圆,如下所示:

I have created an ellipse using matplotlib.patches.ellipse as shown below:

patch = mpatches.Ellipse(center, major_ax, minor_ax, angle_deg, fc='none', ls='solid', ec='g', lw='3.')

我想要的是包含在此补丁中的所有整数坐标的列表.IE.如果我将这个椭圆与同一个网格上的每个整数点一起绘制,有多少个点被包围在椭圆中?

What I want is a list of all the integer coordinates enclosed inside this patch. I.e. If I was to plot this ellipse along with every integer point on the same grid, how many of those points are enclosed in the ellipse?

我试过看看我是否可以提取椭圆的方程,这样我就可以遍历每个点并查看它是否落在这条线上,但我似乎找不到明显的方法来做到这一点,它变得更加复杂因为椭圆的长轴可以以任何角度定向.执行此操作的信息必须存储在某个地方的补丁程序中,但是我似乎找不到它.

I have tried seeing if I can extract the equation of the ellipse so I can loop through each point and see whether it falls within the line but I can't seem to find an obvious way to do this, it becomes more complicated as the major axis of the ellipse can be orientated at any angle. The information to do this must be stored in patches somewhere, but I can't seem to find it.

任何对此的建议将不胜感激.

Any advice on this would be much appreciated.

推荐答案

Ellipse 对象具有方法 contains_point ,如果该点在椭圆中,则返回1,否则返回0.其他方面.

Ellipse objects have a method contains_point which will return 1 if the point is in the ellipse, 0 other wise.

窃取@DrV 的回答:

Stealing from @DrV 's answer:

import matplotlib.pyplot as plt
import matplotlib.patches
import numpy as np

# create an ellipse
el = matplotlib.patches.Ellipse((50,-23), 10, 13.7, 30, facecolor=(1,0,0,.2), edgecolor='none')

# calculate the x and y points possibly within the ellipse
y_int = np.arange(-30, -15)
x_int = np.arange(40, 60)

# create a list of possible coordinates
g = np.meshgrid(x_int, y_int)
coords = list(zip(*(c.flat for c in g)))

# create the list of valid coordinates (from untransformed)
ellipsepoints = np.vstack([p for p in coords if el.contains_point(p, radius=0)])

# just to see if this works
fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_artist(el)
ep = np.array(ellipsepoints)
ax.plot(ellipsepoints[:,0], ellipsepoints[:,1], 'ko')
plt.show()

结果如下:

这篇关于提取由matplotlib补丁括起来的坐标.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆