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 [ ]: