In this blog, we will draw beautiful eyes with the help of the Python matplotlib module.
import matplotlib.pyplot as plt
from numpy import linspace, pi, cos, sin
# -------------- Right eye starts here -------------------
# right eye pupil
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 15)
# --------------Right eye ends here -------------------
plt.show()
# -------------- Right eye starts here -------------------
# right eye pupil
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 15)
# -------------- Right eye ends here -------------------
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
Iris: The iris is the colored part of the eye that surrounds the pupil. It regulates the amount of light that enters the eye.
We have to draw the iris first, then the pupil, because the pupil should come on top of the iris.
# -------------- Right eye starts here -------------------
# right eye iris
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0, marker = "o", color = "#DADADA", markersize = 38)
# right eye pupil
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 15)
# -------------- Right eye ends here -------------------
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
# -------------- Right eye starts here -------------------
# right eye iris
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0, marker = "o", color = "#DADADA", markersize = 38)
# right eye pupil
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 15)
#upper eye line
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 , color = "grey", lw=1)
# -------------- Right eye ends here -------------------
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
Both the curve lines are very far apart. We have to bring it closer to the pupil. To bring it closer to the upper eyelid, we will subtract 0.36 from y. For the lower curve line, we will add 0.6 to y.
# -------------- Right eye starts here -------------------
# right eye iris
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0, marker = "o", color = "#DADADA", markersize = 38)
# right eye pupil
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 15)
#upper eye line
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.36, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
# -------------- Right eye ends here -------------------
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
The right eye pupil and iris are below; we have to move up, so change the y value for that.
# -------------- Right eye starts here -------------------
# right eye iris
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0.35, marker = "o", color = "#DADADA", markersize = 38)
# right eye pupil
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 15)
#upper eye line
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.36, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
# -------------- Right eye ends here -------------------
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
# -------------- Right eye starts here -------------------
# right eye iris
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0.35, marker = "o", color = "#DADADA", markersize = 38)
# right eye pupil
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 15)
#upper eyelid
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.36, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
#one more upper eye line
x3 = linspace(-1, 1, 20)
y3 = cos(x3)
plt.plot(x3, y3 - 0.2, color = "grey", lw=1)
# one more lower eye line
x4 = linspace(-0.9, 0.9, 20)
y4 = -cos(x4)
plt.plot(x4, y4 + 0.71 , color = "grey", lw=0.5)
# -------------- Right eye ends here -------------------
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
Lens: The lens is a clear part of the eye behind the iris that helps to focus light and images on the retina.
# -------------- Right eye starts here -------------------
# right eye iris
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0.35, marker = "o", color = "#DADADA", markersize = 38)
# right eye pupil
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 15)
#upper eyelid
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.36, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
#one more upper eye line
x3 = linspace(-1, 1, 20)
y3 = cos(x3)
plt.plot(x3, y3 - 0.2, color = "grey", lw=1)
# one more lower eye line
x4 = linspace(-0.9, 0.9, 20)
y4 = -cos(x4)
plt.plot(x4, y4 + 0.71 , color = "grey", lw=0.5)
#lens
plt.plot(0.12, 0.35, marker = 'o', color = 'white', markersize = 8)
# -------------- Right eye ends here -------------------
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
We need space on the left side to create the left eye, so let us increase the x-axis limit. Change plt.xlim(-2, 2) to plt.xlim(-5, 2).
# -------------- Right eye starts here -------------------
# right eye iris
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0.35, marker = "o", color = "#DADADA", markersize = 38)
# right eye pupil
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 15)
#upper eyelid
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.36, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
#one more upper eye line
x3 = linspace(-1, 1, 20)
y3 = cos(x3)
plt.plot(x3, y3 - 0.2, color = "grey", lw=1)
# one more lower eye line
x4 = linspace(-0.9, 0.9, 20)
y4 = -cos(x4)
plt.plot(x4, y4 + 0.71 , color = "grey", lw=0.5)
#lens
plt.plot(0.12, 0.35, marker = 'o', color = 'white', markersize = 8)
# -------------- Right eye ends here -------------------
plt.xlim(-5, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
After changing the x-axis limit, the right eyelid position changed. Let us fix that first. Change the position of the eyelid from plt.plot(x1, y1 - 0.36, color = "black", lw = 2) to plt.plot(x1, y1 - 0.28, color = "black", lw = 2).
# -------------- Right eye starts here -------------------
# right eye iris
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0.35, marker = "o", color = "#DADADA", markersize = 38)
# right eye pupil
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 15)
#upper eyelid
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.28, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
#one more upper eye line
x3 = linspace(-1, 1, 20)
y3 = cos(x3)
plt.plot(x3, y3 - 0.2, color = "grey", lw=1)
# one more lower eye line
x4 = linspace(-0.9, 0.9, 20)
y4 = -cos(x4)
plt.plot(x4, y4 + 0.71 , color = "grey", lw=0.5)
#lens
plt.plot(0.12, 0.35, marker = 'o', color = 'white', markersize = 8)
# -------------- Right eye ends here -------------------
plt.xlim(-5, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
# -------------- Right eye starts here -------------------
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0.35, marker = "o", color = "#DADADA", markersize = 38)
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 15)
#upper eye line
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.28, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
#one more upper eye line
x3 = linspace(-1, 1, 20)
y3 = cos(x3)
plt.plot(x3, y3 - 0.2, color = "grey", lw=1)
# one more lower eye line
x4 = linspace(-0.9, 0.9, 20)
y4 = -cos(x4)
plt.plot(x4, y4 + 0.71 , color = "grey", lw=0.5)
# white spot on right eye
plt.plot(0.12, 0.35, marker = 'o', color = 'white', markersize = 8)
# --------------Right eye ends here -------------------
# -------------- Left eye starts here -------------------
plt.plot(-3.1, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(-3.1, 0.35, marker = "o", color = "#DADADA", markersize = 38)
plt.plot(-3.1, 0.35, marker = "o", color = "#000000", markersize = 15)
#left upper eye line
x5 = linspace(-2.1, -4.2, 20)
y5 = -cos(x5)
plt.plot(x5, y5 - 0.28, color = "black", lw=2)
#lower eye line
x6 = linspace(-2.1, -4.1, 20)
y6 = cos(x6)
plt.plot(x6, y6 + 0.64 , color = "grey", lw=1)
#one more lower eye line
x7 = linspace(-2.2, -4.1, 20)
y7 = -cos(x7)
plt.plot(x7, y7 - 0.2 , color = "grey", lw=1)
#one more lower eye line
x8 = linspace(-2.2, -4.1, 20)
y8 = cos(x8)
plt.plot(x8, y8 + 0.75 , color = "grey", lw=0.5)
#white spot on left eye
plt.plot(-3.2, 0.35, marker = 'o', color = 'white', markersize = 8)
# --------------Left eye ends here -------------------
plt.xlim(-5, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
# -------------- Right eye starts here -------------------
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0.35, marker = "o", color = "#DADADA", markersize = 38)
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 15)
#upper eye line
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.28, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
#one more upper eye line
x3 = linspace(-1, 1, 20)
y3 = cos(x3)
plt.plot(x3, y3 - 0.2, color = "grey", lw=1)
# one more lower eye line
x4 = linspace(-0.9, 0.9, 20)
y4 = -cos(x4)
plt.plot(x4, y4 + 0.71 , color = "grey", lw=0.5)
# white spot on right eye
plt.plot(0.12, 0.35, marker = 'o', color = 'white', markersize = 8)
# --------------Right eye ends here -------------------
# -------------- Left eye starts here -------------------
plt.plot(-3.1, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(-3.1, 0.35, marker = "o", color = "#DADADA", markersize = 38)
plt.plot(-3.1, 0.35, marker = "o", color = "#000000", markersize = 15)
#left upper eye line
x5 = linspace(-2.1, -4.2, 20)
y5 = -cos(x5)
plt.plot(x5, y5 - 0.28, color = "black", lw=2)
#lower eye line
x6 = linspace(-2.1, -4.1, 20)
y6 = cos(x6)
plt.plot(x6, y6 + 0.64 , color = "grey", lw=1)
#one more lower eye line
x7 = linspace(-2.2, -4.1, 20)
y7 = -cos(x7)
plt.plot(x7, y7 - 0.2 , color = "grey", lw=1)
#one more lower eye line
x8 = linspace(-2.2, -4.1, 20)
y8 = cos(x8)
plt.plot(x8, y8 + 0.75 , color = "grey", lw=0.5)
#white spot on left eye
plt.plot(-3.2, 0.35, marker = 'o', color = 'white', markersize = 8)
# --------------Left eye ends here -------------------
#red dot on forehead
plt.plot(-1.6, 1, marker = 'o', color = '#F70000', markersize = 30)
plt.xlim(-5, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.show()
# -------------- Right eye starts here -------------------
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(0, 0.35, marker = "o", color = "#DADADA", markersize = 38)
plt.plot(0, 0.35, marker = "o", color = "#000000", markersize = 15)
#upper eye line
x1 = linspace(-1, 1, 20)
y1 = cos(x1)
plt.plot(x1, y1 - 0.28, color = "black", lw=2)
#lower eye line
x2 = linspace(-1, 1, 20)
y2 = -cos(x2)
plt.plot(x2, y2 + 0.6 , color = "grey", lw=1)
#one more upper eye line
x3 = linspace(-1, 1, 20)
y3 = cos(x3)
plt.plot(x3, y3 - 0.2, color = "grey", lw=1)
# one more lower eye line
x4 = linspace(-0.9, 0.9, 20)
y4 = -cos(x4)
plt.plot(x4, y4 + 0.71 , color = "grey", lw=0.5)
# white spot on right eye
plt.plot(0.12, 0.35, marker = 'o', color = 'white', markersize = 8)
# --------------Right eye ends here -------------------
# -------------- Left eye starts here -------------------
plt.plot(-3.1, 0.35, marker = "o", color = "#000000", markersize = 40)
plt.plot(-3.1, 0.35, marker = "o", color = "#DADADA", markersize = 38)
plt.plot(-3.1, 0.35, marker = "o", color = "#000000", markersize = 15)
#left upper eye line
x5 = linspace(-2.1, -4.2, 20)
y5 = -cos(x5)
plt.plot(x5, y5 - 0.28, color = "black", lw=2)
#lower eye line
x6 = linspace(-2.1, -4.1, 20)
y6 = cos(x6)
plt.plot(x6, y6 + 0.64 , color = "grey", lw=1)
#one more lower eye line
x7 = linspace(-2.2, -4.1, 20)
y7 = -cos(x7)
plt.plot(x7, y7 - 0.2 , color = "grey", lw=1)
#one more lower eye line
x8 = linspace(-2.2, -4.1, 20)
y8 = cos(x8)
plt.plot(x8, y8 + 0.75 , color = "grey", lw=0.5)
#white spot on left eye
plt.plot(-3.2, 0.35, marker = 'o', color = 'white', markersize = 8)
# --------------Left eye ends here -------------------
#red dot on forehead
plt.plot(-1.6, 1, marker = 'o', color = '#F70000', markersize = 30)
plt.xlim(-5, 2)
plt.ylim(-2, 2)
plt.gca().set_aspect('equal')
plt.axis("off")
plt.show()