Draw Beautiful Eyes in Python Using the Matplotlib Module

Draw Beautiful Eyes in Python Using the Matplotlib Module

In this blog, we will draw beautiful eyes with the help of the Python matplotlib module.

In [1]:
import matplotlib.pyplot as plt
from numpy import linspace, pi, cos, sin

Draw a right eye

Plot the right eye pupil at (0,0)

In [2]:
# -------------- Right eye starts here -------------------

# right eye pupil
plt.plot(0, 0, marker = "o", color = "#000000", markersize = 15)

# --------------Right eye ends here -------------------

plt.show()

Set x limit, y limit and aspect

In [3]:
# -------------- 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()

Draw iris

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.

In [4]:
# -------------- 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()

Draw the eyelid and lower eye line.

In [5]:
# -------------- 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.

In [6]:
# -------------- 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()

Move pupil and iris up

The right eye pupil and iris are below; we have to move up, so change the y value for that.

In [7]:
# -------------- 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()

Draw one more curve line on the upper and lower side

In [8]:
# -------------- 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()

Draw the lens on the right eye

Lens: The lens is a clear part of the eye behind the iris that helps to focus light and images on the retina.

In [9]:
# -------------- 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()

Create the left eye

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

In [10]:
# -------------- 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).

In [11]:
# -------------- 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()
In [12]:
# -------------- 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()

Red dot on the forehead

In [13]:
# -------------- 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()

Axis off of the figure

In [14]:
# -------------- 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()
In [ ]:
 

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing