Draw Shapes With Points Using Matplotlib Module

Draw Shapes With Points Using Matplotlib Module

In this blog, we will draw different types of shapes using matplotlib module.

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

Set figure size, x-axis and y-axis limit

In [2]:
plt.figure(figsize = (12, 12))

plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a straight line

In this example, we are taking two points and drawing line between them. First point is at (2, 2), where x is 2 and y is also 2. Second point is at (4, 2), where x is 4 and y is 2.

In [3]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)


plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a triangle

In [4]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)


plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a square

In [5]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)

#square
plt.plot(6, 2, marker = 'o', color = 'purple')
plt.plot(8, 2, marker = 'o', color = 'purple')
plt.plot(8, 4, marker = 'o', color = 'purple')
plt.plot(6, 4, marker = 'o', color = 'purple')

plt.plot(
    [6, 8, 8, 6, 6],
    [2, 2, 4, 4, 2],
    color = 'purple'
)


plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a rectangle

In [6]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)

#square
plt.plot(6, 2, marker = 'o', color = 'purple')
plt.plot(8, 2, marker = 'o', color = 'purple')
plt.plot(8, 4, marker = 'o', color = 'purple')
plt.plot(6, 4, marker = 'o', color = 'purple')

plt.plot(
    [6, 8, 8, 6, 6],
    [2, 2, 4, 4, 2],
    color = 'purple'
)

#rectangle
plt.plot(5, 5, marker = 'o', color = 'blue')
plt.plot(9, 5, marker = 'o', color = 'blue')
plt.plot(9, 6, marker = 'o', color = 'blue')
plt.plot(5, 6, marker = 'o', color = 'blue')

plt.plot(
    [5, 9, 9, 5, 5],
    [5, 5, 6, 6, 5],
    color = 'blue'
)

plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a pentagon

In [7]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)

#square
plt.plot(6, 2, marker = 'o', color = 'purple')
plt.plot(8, 2, marker = 'o', color = 'purple')
plt.plot(8, 4, marker = 'o', color = 'purple')
plt.plot(6, 4, marker = 'o', color = 'purple')

plt.plot(
    [6, 8, 8, 6, 6],
    [2, 2, 4, 4, 2],
    color = 'purple'
)

#rectangle
plt.plot(5, 5, marker = 'o', color = 'blue')
plt.plot(9, 5, marker = 'o', color = 'blue')
plt.plot(9, 6, marker = 'o', color = 'blue')
plt.plot(5, 6, marker = 'o', color = 'blue')

plt.plot(
    [5, 9, 9, 5, 5],
    [5, 5, 6, 6, 5],
    color = 'blue'
)

#pentagon
plt.plot(6, 6.5, marker = 'o', color = 'violet')
plt.plot(8, 6.5, marker = 'o', color = 'violet')
plt.plot(8.5, 8, marker = 'o', color = 'violet')
plt.plot(7, 9, marker = 'o', color = 'violet')
plt.plot(5.5, 8, marker = 'o', color = 'violet')

plt.plot(
    [6, 8, 8.5, 7, 5.5, 6],
    [6.5, 6.5, 8, 9, 8, 6.5],
    color = 'violet'
)


plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a star

In [8]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)

#square
plt.plot(6, 2, marker = 'o', color = 'purple')
plt.plot(8, 2, marker = 'o', color = 'purple')
plt.plot(8, 4, marker = 'o', color = 'purple')
plt.plot(6, 4, marker = 'o', color = 'purple')

plt.plot(
    [6, 8, 8, 6, 6],
    [2, 2, 4, 4, 2],
    color = 'purple'
)

#rectangle
plt.plot(5, 5, marker = 'o', color = 'blue')
plt.plot(9, 5, marker = 'o', color = 'blue')
plt.plot(9, 6, marker = 'o', color = 'blue')
plt.plot(5, 6, marker = 'o', color = 'blue')

plt.plot(
    [5, 9, 9, 5, 5],
    [5, 5, 6, 6, 5],
    color = 'blue'
)

#pentagon
plt.plot(6, 6.5, marker = 'o', color = 'violet')
plt.plot(8, 6.5, marker = 'o', color = 'violet')
plt.plot(8.5, 8, marker = 'o', color = 'violet')
plt.plot(7, 9, marker = 'o', color = 'violet')
plt.plot(5.5, 8, marker = 'o', color = 'violet')

plt.plot(
    [6, 8, 8.5, 7, 5.5, 6],
    [6.5, 6.5, 8, 9, 8, 6.5],
    color = 'violet'
)

#star
plt.plot(4.5, 7, marker = 'o', color = '#E7008E')
plt.plot(3.5, 9.5, marker = 'o', color = '#E7008E')
plt.plot(2.5, 7, marker = 'o', color = '#E7008E')
plt.plot(5, 8.5, marker = 'o', color = '#E7008E')
plt.plot(2, 8.5, marker = 'o', color = '#E7008E')

plt.plot(
    [4.5, 3.5, 2.5, 5, 2, 4.5],
    [7, 9.5, 7, 8.5, 8.5, 7],
    color = '#E7008E'
)

plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a circle

In [9]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)

#square
plt.plot(6, 2, marker = 'o', color = 'purple')
plt.plot(8, 2, marker = 'o', color = 'purple')
plt.plot(8, 4, marker = 'o', color = 'purple')
plt.plot(6, 4, marker = 'o', color = 'purple')

plt.plot(
    [6, 8, 8, 6, 6],
    [2, 2, 4, 4, 2],
    color = 'purple'
)

#rectangle
plt.plot(5, 5, marker = 'o', color = 'blue')
plt.plot(9, 5, marker = 'o', color = 'blue')
plt.plot(9, 6, marker = 'o', color = 'blue')
plt.plot(5, 6, marker = 'o', color = 'blue')

plt.plot(
    [5, 9, 9, 5, 5],
    [5, 5, 6, 6, 5],
    color = 'blue'
)

#pentagon
plt.plot(6, 6.5, marker = 'o', color = 'violet')
plt.plot(8, 6.5, marker = 'o', color = 'violet')
plt.plot(8.5, 8, marker = 'o', color = 'violet')
plt.plot(7, 9, marker = 'o', color = 'violet')
plt.plot(5.5, 8, marker = 'o', color = 'violet')

plt.plot(
    [6, 8, 8.5, 7, 5.5, 6],
    [6.5, 6.5, 8, 9, 8, 6.5],
    color = 'violet'
)

#star
plt.plot(4.5, 7, marker = 'o', color = '#E7008E')
plt.plot(3.5, 9.5, marker = 'o', color = '#E7008E')
plt.plot(2.5, 7, marker = 'o', color = '#E7008E')
plt.plot(5, 8.5, marker = 'o', color = '#E7008E')
plt.plot(2, 8.5, marker = 'o', color = '#E7008E')

plt.plot(
    [4.5, 3.5, 2.5, 5, 2, 4.5],
    [7, 9.5, 7, 8.5, 8.5, 7],
    color = '#E7008E'
)

#circle
angles = linspace(0 * pi, 2 * pi, 100 )
r = 1.2
xs = r * cos(angles)
ys = r * sin(angles)
plt.plot(xs + 10, ys + 3, color = '#4D04B1')

plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a oval

In [10]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)

#square
plt.plot(6, 2, marker = 'o', color = 'purple')
plt.plot(8, 2, marker = 'o', color = 'purple')
plt.plot(8, 4, marker = 'o', color = 'purple')
plt.plot(6, 4, marker = 'o', color = 'purple')

plt.plot(
    [6, 8, 8, 6, 6],
    [2, 2, 4, 4, 2],
    color = 'purple'
)

#rectangle
plt.plot(5, 5, marker = 'o', color = 'blue')
plt.plot(9, 5, marker = 'o', color = 'blue')
plt.plot(9, 6, marker = 'o', color = 'blue')
plt.plot(5, 6, marker = 'o', color = 'blue')

plt.plot(
    [5, 9, 9, 5, 5],
    [5, 5, 6, 6, 5],
    color = 'blue'
)

#pentagon
plt.plot(6, 6.5, marker = 'o', color = 'violet')
plt.plot(8, 6.5, marker = 'o', color = 'violet')
plt.plot(8.5, 8, marker = 'o', color = 'violet')
plt.plot(7, 9, marker = 'o', color = 'violet')
plt.plot(5.5, 8, marker = 'o', color = 'violet')

plt.plot(
    [6, 8, 8.5, 7, 5.5, 6],
    [6.5, 6.5, 8, 9, 8, 6.5],
    color = 'violet'
)

#star
plt.plot(4.5, 7, marker = 'o', color = '#E7008E')
plt.plot(3.5, 9.5, marker = 'o', color = '#E7008E')
plt.plot(2.5, 7, marker = 'o', color = '#E7008E')
plt.plot(5, 8.5, marker = 'o', color = '#E7008E')
plt.plot(2, 8.5, marker = 'o', color = '#E7008E')

plt.plot(
    [4.5, 3.5, 2.5, 5, 2, 4.5],
    [7, 9.5, 7, 8.5, 8.5, 7],
    color = '#E7008E'
)

#circle
angles = linspace(0 * pi, 2 * pi, 100 )
r = 1.2
xs = r * cos(angles)
ys = r * sin(angles)
plt.plot(xs + 10, ys + 3, color = '#4D04B1')

#oval
oval_angles = linspace(0 * pi, 2 * pi, 100 )
r1 = 1.5
r2 = 1
xs = r1 * cos(oval_angles)
ys = r2 * sin(oval_angles)
plt.plot(xs + 6, ys + 10.5, color = '#74D55B', lw = 3)


plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a diamond

In [11]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)

#square
plt.plot(6, 2, marker = 'o', color = 'purple')
plt.plot(8, 2, marker = 'o', color = 'purple')
plt.plot(8, 4, marker = 'o', color = 'purple')
plt.plot(6, 4, marker = 'o', color = 'purple')

plt.plot(
    [6, 8, 8, 6, 6],
    [2, 2, 4, 4, 2],
    color = 'purple'
)

#rectangle
plt.plot(5, 5, marker = 'o', color = 'blue')
plt.plot(9, 5, marker = 'o', color = 'blue')
plt.plot(9, 6, marker = 'o', color = 'blue')
plt.plot(5, 6, marker = 'o', color = 'blue')

plt.plot(
    [5, 9, 9, 5, 5],
    [5, 5, 6, 6, 5],
    color = 'blue'
)

#pentagon
plt.plot(6, 6.5, marker = 'o', color = 'violet')
plt.plot(8, 6.5, marker = 'o', color = 'violet')
plt.plot(8.5, 8, marker = 'o', color = 'violet')
plt.plot(7, 9, marker = 'o', color = 'violet')
plt.plot(5.5, 8, marker = 'o', color = 'violet')

plt.plot(
    [6, 8, 8.5, 7, 5.5, 6],
    [6.5, 6.5, 8, 9, 8, 6.5],
    color = 'violet'
)

#star
plt.plot(4.5, 7, marker = 'o', color = '#E7008E')
plt.plot(3.5, 9.5, marker = 'o', color = '#E7008E')
plt.plot(2.5, 7, marker = 'o', color = '#E7008E')
plt.plot(5, 8.5, marker = 'o', color = '#E7008E')
plt.plot(2, 8.5, marker = 'o', color = '#E7008E')

plt.plot(
    [4.5, 3.5, 2.5, 5, 2, 4.5],
    [7, 9.5, 7, 8.5, 8.5, 7],
    color = '#E7008E'
)

#circle
angles = linspace(0 * pi, 2 * pi, 100 )
r = 1.2
xs = r * cos(angles)
ys = r * sin(angles)
plt.plot(xs + 10, ys + 3, color = '#4D04B1')

#oval
oval_angles = linspace(0 * pi, 2 * pi, 100 )
r1 = 1.5
r2 = 1
xs = r1 * cos(oval_angles)
ys = r2 * sin(oval_angles)
plt.plot(xs + 6, ys + 10.5, color = '#74D55B', lw = 3)

#diamond
plt.plot(10, 11, marker = 'o', color = '#5100A6')
plt.plot(11, 9.5, marker = 'o', color = '#5100A6')
plt.plot(10, 8, marker = 'o', color = '#5100A6')
plt.plot(9, 9.5, marker = 'o', color = '#5100A6')

plt.plot(
    [10, 11, 10, 9, 10],
    [11, 9.5, 8, 9.5, 11],
    color = '#5100A6'
)

plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
plt.show()

Draw a heart

In [12]:
plt.figure(figsize = (12, 12))

#straight line
plt.plot(2, 2, marker = 'o', color = 'red')
plt.plot(4, 2, marker = 'o', color = 'red')

plt.plot(
    [2, 4],
    [2, 2],
    color = 'red'
)

#triangle
plt.plot(2, 4, marker = 'o', color = 'green')
plt.plot(4, 4, marker = 'o', color = 'green')
plt.plot(3, 6, marker = 'o', color = 'green')

plt.plot(
    [2, 4, 3, 2],
    [4, 4, 6, 4],
    color = 'green'
)

#square
plt.plot(6, 2, marker = 'o', color = 'purple')
plt.plot(8, 2, marker = 'o', color = 'purple')
plt.plot(8, 4, marker = 'o', color = 'purple')
plt.plot(6, 4, marker = 'o', color = 'purple')

plt.plot(
    [6, 8, 8, 6, 6],
    [2, 2, 4, 4, 2],
    color = 'purple'
)

#rectangle
plt.plot(5, 5, marker = 'o', color = 'blue')
plt.plot(9, 5, marker = 'o', color = 'blue')
plt.plot(9, 6, marker = 'o', color = 'blue')
plt.plot(5, 6, marker = 'o', color = 'blue')

plt.plot(
    [5, 9, 9, 5, 5],
    [5, 5, 6, 6, 5],
    color = 'blue'
)

#pentagon
plt.plot(6, 6.5, marker = 'o', color = 'violet')
plt.plot(8, 6.5, marker = 'o', color = 'violet')
plt.plot(8.5, 8, marker = 'o', color = 'violet')
plt.plot(7, 9, marker = 'o', color = 'violet')
plt.plot(5.5, 8, marker = 'o', color = 'violet')

plt.plot(
    [6, 8, 8.5, 7, 5.5, 6],
    [6.5, 6.5, 8, 9, 8, 6.5],
    color = 'violet'
)

#star
plt.plot(4.5, 7, marker = 'o', color = '#E7008E')
plt.plot(3.5, 9.5, marker = 'o', color = '#E7008E')
plt.plot(2.5, 7, marker = 'o', color = '#E7008E')
plt.plot(5, 8.5, marker = 'o', color = '#E7008E')
plt.plot(2, 8.5, marker = 'o', color = '#E7008E')

plt.plot(
    [4.5, 3.5, 2.5, 5, 2, 4.5],
    [7, 9.5, 7, 8.5, 8.5, 7],
    color = '#E7008E'
)

#circle
angles = linspace(0 * pi, 2 * pi, 100 )
r = 1.2
xs = r * cos(angles)
ys = r * sin(angles)
plt.plot(xs + 10, ys + 3, color = '#4D04B1')

#oval
oval_angles = linspace(0 * pi, 2 * pi, 100 )
r1 = 1.5
r2 = 1
xs = r1 * cos(oval_angles)
ys = r2 * sin(oval_angles)
plt.plot(xs + 6, ys + 10.5, color = '#74D55B', lw = 3)

#diamond
plt.plot(10, 11, marker = 'o', color = '#5100A6')
plt.plot(11, 9.5, marker = 'o', color = '#5100A6')
plt.plot(10, 8, marker = 'o', color = '#5100A6')
plt.plot(9, 9.5, marker = 'o', color = '#5100A6')

plt.plot(
    [10, 11, 10, 9, 10],
    [11, 9.5, 8, 9.5, 11],
    color = '#5100A6'
)

#heart
plt.plot(10.5, 6, marker="$\u2665$", color='red', markersize = 120)

plt.xlim(1, 12)
plt.ylim(1, 12)
plt.gca().set_aspect('equal')
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