hel*_*qua 4 python patch matplotlib ellipse
我使用matplotlib.patches.ellipse创建了一个椭圆,如下所示:
patch = mpatches.Ellipse(center, major_ax, minor_ax, angle_deg, fc='none', ls='solid', ec='g', lw='3.')
Run Code Online (Sandbox Code Playgroud)
我想要的是此修补程序内包含的所有整数坐标的列表。即,如果我要将此椭圆与每个整数点一起绘制在同一网格上,那么椭圆中将包含哪些点?
我尝试查看是否可以提取椭圆方程,以便可以遍历每个点并查看其是否在直线内,但是我似乎找不到一种明显的方法来完成此操作,因为主椭圆的轴可以以任何角度定向。必须将这些信息存储在某个地方的补丁程序中,但是我似乎找不到它。
任何对此的建议将不胜感激。
Ellipse对象有一个方法contains_point,如果该点在椭圆中,则将返回1,否则将返回0。
从@DrV的答案中窃取:
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()
Run Code Online (Sandbox Code Playgroud)
这将为您提供如下结果:
| 归档时间: |
|
| 查看次数: |
2598 次 |
| 最近记录: |