我正在绘制一个椭圆并使用translate
函数对其进行平移,该函数是手动定义的dx & dy
现在,我想在此数组中包含更多具有不同dx,dy
值的图。
translation_points =
[ (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12)]
我该怎么做?
import numpy as np
import matplotlib.pyplot as plt
def ellipse(x, y):
value = (x*x) + (y*y)/3
if (value >= 300):
return 0
else:
return 1
def translate(x, y):
DX = 5
DY = 5
return (x- DX, y - DY)
def rotate(x, y):
theta = np.radians(40)
matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
return np.dot(matrix, (x,y))
data = np.zeros((100,100))
for i in range(0, 100):
for j in range(0, 100):
(x, y) = translate(i,j)
(x, y) = rotate(x, y)
data[i,j] = ellipse(x, y)
plt.imshow(data, cmap="gray")
plt.show()
答案 0 :(得分:2)
首先,修改您的translate()
,添加新参数offset
:
def translate(x, y, offset):
(dx, dy) = offset
return x - dx, y - dy
然后将2个for
循环放入一个函数中,以便稍后我们可以调用它,该函数也应该接受参数offset
。然后我们可以调用它来绘制每个偏移量。
def draw(offset):
data = np.zeros((100, 100))
for i in range(-100, 100):
for j in range(-100, 100):
(x, y) = translate(i, j, offset)
(x, y) = rotate(x, y)
data[i, j] = ellipse(x, y)
plt.imshow(data, cmap="gray")
最后,创建一个循环,为translation_points
中的每个偏移量绘制椭圆。在这里,我使用plt.subplot(4, 7, i+1)
创建28个子图,每个子图是一个翻译的椭圆。如果您只想查看每个单独的图,则可以注释此行。
for i in range(len(translation_points)):
plt.subplot(4, 7, i+1)
draw(translation_points[i])
是的,我们做到了。
由于我们使用imshow
,因此将裁剪图。此外,坐标是完全错误的。因此,请先设置范围:
for i in range(-100, 100):
for j in range(-100, 100):
然后给它一些默认的偏移量:
def translate(x, y, offset):
(dx, dy) = offset
return x - dx - 50, y - dy - 50
扩展图形,设置轴限制:在draw()
中添加这些行
plt.xlim(-50, 50)
plt.ylim(-50, 50)
plt.imshow(data, cmap="cool", extent=[-data.shape[1]/2., data.shape[1]/2., -data.shape[0]/2., data.shape[0]/2.])
最后:
import numpy as np
import matplotlib.pyplot as plt
translation_points = [(5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11),
(5, 12), (5, 13), (5, 14), (6, 5), (6, 6), (6, 7),
(6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13),
(6, 14), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9),
(7, 10), (7, 11), (7, 12)]
def ellipse(x, y):
value = (x*x) + (y*y)/3
if value >= 300:
return 0
else:
return 1
def translate(x, y, offset):
# dx = 5
# dy = 5
(dx, dy) = offset
return x - dx - 50, y - dy - 50
def rotate(x, y):
theta = np.radians(40)
matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
return np.dot(matrix, (x, y))
def draw(offset):
data = np.zeros((100, 100))
for i in range(-100, 100):
for j in range(-100, 100):
(x, y) = translate(i, j, offset)
(x, y) = rotate(x, y)
data[i, j] = ellipse(x, y)
plt.xlim(-50, 50)
plt.ylim(-50, 50)
plt.imshow(data, cmap="gray",
extent=[-data.shape[1]/2., data.shape[1]/2.,
-data.shape[0]/2., data.shape[0]/2.])
for i in range(len(translation_points)):
plt.subplot(4, 7, i+1)
draw(translation_points[i])
plt.show()
答案 1 :(得分:1)
import numpy as np
import matplotlib.pyplot as plt
translation_points = [ (5, 6), (5, 7), (10,10), (20, 8), (5, 9), (12, 10), (40, 40), (50, 50),(20, 8)]
def ellipse(x, y):
value = (x*x) + (y*y)/3
if (value >= 300):
return 0
else:
return 1
def translate(x, y,a,b):
#print a,b
DX = a
DY = b
return (x- DX, y - DY)
def rotate(x, y):
theta = np.radians(40)
matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
return np.dot(matrix, (x,y))
def create(tlpoints):
a,b=tlpoints
#print a,b
data = np.zeros((100,100))
for i in range(0, 100):
for j in range(0, 100):
(x, y) = translate(i,j,a,b)
(x, y) = rotate(x, y)
data[i,j] = ellipse(x, y)
return data
ells=[create(translation_points[i]) for i in range(len(translation_points))]
fig=plt.figure(figsize=(10,10))
columns = 3
rows = len(translation_points)/columns
for i in range(1, columns*rows +1):
fig.add_subplot(rows, columns, i)
plt.imshow(ells[i-1],cmap='gray')
plt.show()