M.Bore
M.Bore

Reputation: 97

How can I plot an ellipse over different centers defined in an array

I am plotting an ellipse and the translating it using the translate function where are define manually dx & dy

Now I would like to have more plots with different values of dx,dy which are contained in this array.

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)]

How can I do that?

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()

Upvotes: 0

Views: 225

Answers (2)

AcaNg
AcaNg

Reputation: 706

first, modify your translate(), add a new parameter offset:

def translate(x, y, offset):
    (dx, dy) = offset
    return x - dx, y - dy

then put the 2 for loops in a function so we can call it later, this function should accept a parameter offset too. then we can call it to plot for each 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")

finally, create a loop that plot our ellipse for each offset in translation_points. here i use plt.subplot(4, 7, i+1) to create 28 subplots, each subplots is for a translated ellipse. you can comment this line if you just want to see each separate plot.

for i in range(len(translation_points)):
    plt.subplot(4, 7, i+1)
    draw(translation_points[i])

yeah, we did it.


more edit:

since we use imshow, the plots are cropped. moreover, the coordinates are totally wrong. so first set our range:

for i in range(-100, 100):
    for j in range(-100, 100):

then give it some default offset:

def translate(x, y, offset):
    (dx, dy) = offset
    return x - dx - 50, y - dy - 50

extend the figure, set axis limits: add these lines in 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.])

and finally:

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()

Upvotes: 1

Rahul Verma
Rahul Verma

Reputation: 3176

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()

output

Upvotes: 1

Related Questions