虽然多边形图中的点 points/lines 失败(Circle.contains_point;Python;matplotlib;pyplot)
While point in polygon plot points/lines fails (Circle.contains_point; Python; matplotlib; pyplot)
我的任务:绘制一个初始点并计算新的点位置(x,y),同时点在一个圆上。
我的问题: 使用 while
循环执行此操作无法 运行 代码块,因为它错误地确定圆不包含点(图 1)。然而,使用 for
循环可以看出多个点位于圆中,但仍然错误地识别出圆中不存在点(图 2)。
附加信息: 我似乎也无法得到线而不是要绘制的点(例如使用 pyplot.plot(x, y, '-')
)。
如果有更精简的方法,例如绘制 circle/points/lines 或整个方法,我很想看看它们。我很感激能提供的任何帮助。
在这里和其他地方进行了大量搜索后,我得到了以下代码:
# IMPORT LIBRARIES ####
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mPath
import matplotlib.patches as mPatches
# CREATE CIRCLE ####
# Input circle radius
circleRadius = 2
# Create circle (arguments: centre, radius, face colour (fc), edge colour (ec), alpha)
circle = mPatches.Circle((0, 0), radius = circleRadius, fc = 'm', alpha = 0.3)
# Plot circle
fig1, ax = plt.subplots()
ax.add_patch(circle)
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.set_aspect('equal')
# CREATE POINTS ####
# Input coordinates
x0 = -1
y0 = 0.5
# Assignment for equation
x = x0
y = y0
# Store
curr_pt = [x, y]
counter = 0
# Plot 1st point (x, y)
plt.plot(x, y, '.c')
# Pre-checks
print(circle.contains_point([x, y]))
print(counter)
print(x0, y0)
print(x, y)
# Plot points
while circle.contains_point([x, y]): # see 1st figure
#for i in range(5): # uncomment this line (for) and comment above line (while) see 2nd figure
xnew = x**2 - y**2 + x0
ynew = 2 * x * y + y0
x = xnew
y = ynew
# Update current point
curr_pt = [x, y]
# Plot point (x, y)
plt.plot(x, y, '.c')
counter += 1
# Post-checks
print(circle.contains_point([x, y]))
print(counter)
print(x0, y0)
print(x, y)
print(xnew, ynew) # note this check will output error with while loop as they will not be defined
# Print plot
plt.show()
输出:
# while loop (Fig 1)
False
0
-1 0.5
-1 0.5
False
0
-1 0.5
-1 0.5
Fig 1 (while loop)
# for loop (Fig 2)
False
0
-1 0.5
-1 0.5
False
5
-1 0.5
5.063203536206856 -4.162733919918537
5.063203536206856 -4.162733919918537
Fig 2 (for loop)
更新:
利用这些答案中概述的方法:
我修改后的代码现在可以正确识别点是否在 Circle
中,并根据 contains_point()
.
执行 while
循环
修改后的代码:
# IMPORT LIBRARIES ####
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mPath
import matplotlib.patches as mPatches
# CREATE CIRCLE ####
# Input circle radius
circleRadius = 2
# Create circle (arguments: centre, radius, face colour (fc), edge colour (ec), alpha)
circle = mPatches.Circle((0, 0), radius = circleRadius, fc = 'm', alpha = 0.3)
# Get path and 2D affine transformation
pathC = circle.get_path()
transformC = circle.get_transform()
# Apply transformation to the path
transPath = transformC.transform_path(pathC)
# Get path of transformed circle
polygon = mPatches.PathPatch(transPath, fc = 'm', alpha = 0.3)
# CREATE POINTS ####
# Input initial coordinates
x0 = -1
y0 = 0.5
# Assignment for new point equation
x = x0
y = y0
# Store
curr_pt = (x, y)
all_x = []
all_y = []
# Iterations required to escape circle
counter = 0
# Pre-check
cp1 = polygon.contains_point(curr_pt)
print("Pre-loop checks:")
print("Polygon contains point:", cp1)
print("Counter:", counter)
print("(x, y):", x, y)
print("")
# Create figure and single subplot
fig, ax = plt.subplots()
# Plot initial point (x, y)
ax.scatter(curr_pt[0], curr_pt[1], color = 'c')
# Plot new points
while polygon.contains_point(curr_pt):
xnew = x**2 - y**2 + x0
ynew = 2 * x * y + y0
x = xnew
y = ynew
# Update current point
curr_pt = (x, y)
# Update x, y lists for plotting lines
all_x.append(curr_pt[0])
all_y.append(curr_pt[1])
# Plot new point
ax.scatter(curr_pt[0], curr_pt[1], color = 'c')
# Increment counter
counter += 1
if counter == 50:
break
# Post-check
cp2 = polygon.contains_point(curr_pt)
print("Post-loop checks:")
print("Polygon contains point:", cp2)
print("Counter:", counter)
print("(x, y):", x, y)
print("(xnew, ynew):", xnew, ynew)
# CREATE PLOT ####
# Plot lines
ax.plot(all_x, all_y, color = 'c')
# Plot circle
ax.add_patch(polygon)
# Print plot
ax.set_aspect(1.0)
fig.savefig("Correct point in polygon (points and lines).png")
plt.show()
输出:
Pre-loop checks:
Polygon contains point: True
Counter: 0
(x, y): -1 0.5
Post-loop checks:
Polygon contains point: False
Counter: 4
(x, y): -2.6183929443359375 0.890380859375
(xnew, ynew): -2.6183929443359375 0.890380859375
我的任务:绘制一个初始点并计算新的点位置(x,y),同时点在一个圆上。
我的问题: 使用 while
循环执行此操作无法 运行 代码块,因为它错误地确定圆不包含点(图 1)。然而,使用 for
循环可以看出多个点位于圆中,但仍然错误地识别出圆中不存在点(图 2)。
附加信息: 我似乎也无法得到线而不是要绘制的点(例如使用 pyplot.plot(x, y, '-')
)。
如果有更精简的方法,例如绘制 circle/points/lines 或整个方法,我很想看看它们。我很感激能提供的任何帮助。
在这里和其他地方进行了大量搜索后,我得到了以下代码:
# IMPORT LIBRARIES ####
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mPath
import matplotlib.patches as mPatches
# CREATE CIRCLE ####
# Input circle radius
circleRadius = 2
# Create circle (arguments: centre, radius, face colour (fc), edge colour (ec), alpha)
circle = mPatches.Circle((0, 0), radius = circleRadius, fc = 'm', alpha = 0.3)
# Plot circle
fig1, ax = plt.subplots()
ax.add_patch(circle)
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.set_aspect('equal')
# CREATE POINTS ####
# Input coordinates
x0 = -1
y0 = 0.5
# Assignment for equation
x = x0
y = y0
# Store
curr_pt = [x, y]
counter = 0
# Plot 1st point (x, y)
plt.plot(x, y, '.c')
# Pre-checks
print(circle.contains_point([x, y]))
print(counter)
print(x0, y0)
print(x, y)
# Plot points
while circle.contains_point([x, y]): # see 1st figure
#for i in range(5): # uncomment this line (for) and comment above line (while) see 2nd figure
xnew = x**2 - y**2 + x0
ynew = 2 * x * y + y0
x = xnew
y = ynew
# Update current point
curr_pt = [x, y]
# Plot point (x, y)
plt.plot(x, y, '.c')
counter += 1
# Post-checks
print(circle.contains_point([x, y]))
print(counter)
print(x0, y0)
print(x, y)
print(xnew, ynew) # note this check will output error with while loop as they will not be defined
# Print plot
plt.show()
输出:
# while loop (Fig 1)
False
0
-1 0.5
-1 0.5
False
0
-1 0.5
-1 0.5
Fig 1 (while loop)
# for loop (Fig 2)
False
0
-1 0.5
-1 0.5
False
5
-1 0.5
5.063203536206856 -4.162733919918537
5.063203536206856 -4.162733919918537
Fig 2 (for loop)
更新:
利用这些答案中概述的方法:
我修改后的代码现在可以正确识别点是否在 Circle
中,并根据 contains_point()
.
while
循环
修改后的代码:
# IMPORT LIBRARIES ####
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mPath
import matplotlib.patches as mPatches
# CREATE CIRCLE ####
# Input circle radius
circleRadius = 2
# Create circle (arguments: centre, radius, face colour (fc), edge colour (ec), alpha)
circle = mPatches.Circle((0, 0), radius = circleRadius, fc = 'm', alpha = 0.3)
# Get path and 2D affine transformation
pathC = circle.get_path()
transformC = circle.get_transform()
# Apply transformation to the path
transPath = transformC.transform_path(pathC)
# Get path of transformed circle
polygon = mPatches.PathPatch(transPath, fc = 'm', alpha = 0.3)
# CREATE POINTS ####
# Input initial coordinates
x0 = -1
y0 = 0.5
# Assignment for new point equation
x = x0
y = y0
# Store
curr_pt = (x, y)
all_x = []
all_y = []
# Iterations required to escape circle
counter = 0
# Pre-check
cp1 = polygon.contains_point(curr_pt)
print("Pre-loop checks:")
print("Polygon contains point:", cp1)
print("Counter:", counter)
print("(x, y):", x, y)
print("")
# Create figure and single subplot
fig, ax = plt.subplots()
# Plot initial point (x, y)
ax.scatter(curr_pt[0], curr_pt[1], color = 'c')
# Plot new points
while polygon.contains_point(curr_pt):
xnew = x**2 - y**2 + x0
ynew = 2 * x * y + y0
x = xnew
y = ynew
# Update current point
curr_pt = (x, y)
# Update x, y lists for plotting lines
all_x.append(curr_pt[0])
all_y.append(curr_pt[1])
# Plot new point
ax.scatter(curr_pt[0], curr_pt[1], color = 'c')
# Increment counter
counter += 1
if counter == 50:
break
# Post-check
cp2 = polygon.contains_point(curr_pt)
print("Post-loop checks:")
print("Polygon contains point:", cp2)
print("Counter:", counter)
print("(x, y):", x, y)
print("(xnew, ynew):", xnew, ynew)
# CREATE PLOT ####
# Plot lines
ax.plot(all_x, all_y, color = 'c')
# Plot circle
ax.add_patch(polygon)
# Print plot
ax.set_aspect(1.0)
fig.savefig("Correct point in polygon (points and lines).png")
plt.show()
输出:
Pre-loop checks:
Polygon contains point: True
Counter: 0
(x, y): -1 0.5
Post-loop checks:
Polygon contains point: False
Counter: 4
(x, y): -2.6183929443359375 0.890380859375
(xnew, ynew): -2.6183929443359375 0.890380859375