Draw Properties of Triangle Using the Python Matplotlib Module

Draw Properties of Triangle Using the Python Matplotlib Module

In this blog, we will draw properties of triangle using matplotlib module. In this way, students can easily visualize the properties of triangles and learn faster.

Import matplotlib.pyplot module

In [1]:
import matplotlib.pyplot as plt

Plot a point at (0,0)

In [2]:
plt.plot(0, 0, color = 'red', marker = 'o', markersize = 10)
plt.show()

Plot two more points and connect all three points

First point(0, 0) shift to left 5. Then create two more points and connect all three points so that it will become a triangle.

In [3]:
plt.plot(-5, 0, marker = 'o', markersize = 10)
plt.plot(10, 0, marker = 'o', markersize = 10)
plt.plot(0, 8, marker = 'o', markersize = 10)

#connecting all three points to make triangle
plt.plot( 
    (-5, 10, 0, -5),   
    (0, 0, 8, 0)    
)

plt.show()

Write Name of the points like 'A', 'B', 'C'.

In [4]:
plt.plot(-5, 0, marker = 'o', markersize = 10)
plt.plot(10, 0, marker = 'o', markersize = 10)
plt.plot(0, 8, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (-5, 10, 0, -5),   
    (0, 0, 8, 0)    
)

# name of the points 
plt.text(-5.9, 0, 'A')
plt.text(10.4, 0, 'B')
plt.text(0, 8.3, 'C')

plt.show()

Set the x-limit and y-limit

In [5]:
plt.plot(-5, 0, marker = 'o', markersize = 10)
plt.plot(10, 0, marker = 'o', markersize = 10)
plt.plot(0, 8, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (-5, 10, 0, -5),   
    (0, 0, 8, 0)    
)

# name of the points 
plt.text(-5.9, 0, 'A')
plt.text(10.4, 0, 'B')
plt.text(0, 8.3, 'C')

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()

Change x and y coordinates in variables

We will change all x and y coordnates in variables.

In [6]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 0.9, y1, 'A')
plt.text(x2 + 0.4, y2, 'B')
plt.text(x3, y3 + 0.3, 'C')

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()

Find the mid point of AB and draw median

A median of a triangle is a straight line through a vertex and the midpoint of the opposite side, and divides the triangle into two equal areas.

Draw the mid point of AB

In [7]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 0.9, y1, 'A')
plt.text(x2 + 0.4, y2, 'B')
plt.text(x3, y3 + 0.3, 'C')


#mid point of AB
m1x = (x1 + x2)/2
m1y = (y1 + y2)/2
plt.plot(m1x, m1y, marker = 'o', color = 'red')

plt.text(m1x, m1y-.5, 'M1')


#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()

Draw the median

In [8]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 0.9, y1, 'A')
plt.text(x2 + 0.4, y2, 'B')
plt.text(x3, y3 + 0.3, 'C')


#mid point of AB
m1x = (x1 + x2)/2
m1y = (y1 + y2)/2
plt.plot(m1x, m1y, marker = 'o', color = 'red')

plt.text(m1x, m1y-.5, 'M1')

#median 1
plt.plot(
    (x3, m1x ),     
    (y3, m1y )
)

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()

Find the mid point of AC and median

In [9]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 0.9, y1, 'A')
plt.text(x2 + 0.4, y2, 'B')
plt.text(x3, y3 + 0.3, 'C')


#middle point of AB
m1x = (x1 + x2)/2
m1y = (y1 + y2)/2
plt.plot(m1x, m1y, marker = 'o', color = 'red')

plt.text(m1x, m1y-.5, 'M1')

#median from C to AB
plt.plot(
    (x3, m1x ),     
    (y3, m1y )
)

#middle point of AC
m2x = (x1 + x3) / 2
m2y = (y1 + y3) / 2

plt.plot(m2x, m2y, marker = 'o', color = 'black')
plt.text(m2x - 1.5, m2y, 'M2')

#middle point of B to AC
plt.plot(
    (x2, m2x),
    (y2, m2y)
)

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()

Find the mid point of BC and median

In [10]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 0.9, y1, 'A')
plt.text(x2 + 0.4, y2, 'B')
plt.text(x3, y3 + 0.3, 'C')


#middle point of AB
m1x = (x1 + x2)/2
m1y = (y1 + y2)/2
plt.plot(m1x, m1y, marker = 'o', color = 'red')

plt.text(m1x, m1y-.5, 'M1')

#median from C to AB
plt.plot(
    (x3, m1x ),     
    (y3, m1y )
)

#middle point of AC
m2x = (x1 + x3) / 2
m2y = (y1 + y3) / 2

plt.plot(m2x, m2y, marker = 'o', color = 'black')
plt.text(m2x - 1.5, m2y, 'M2')

#middle point of B to AC
plt.plot(
    (x2, m2x),
    (y2, m2y)
)

#median from A to BC
m3x = (x2 + x3)/2
m3y = (y2 + y3)/2
plt.plot(m3x , m3y, marker= 'o' , color ='violet')
plt.text(m3x , m3y , 'M3')

plt.plot( 
    (x1, m3x), 
    (y1, m3y)
)

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()

Plot Centroid

The three medians intersect at a single point, that point is the triangle's centroid.

In [11]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 0.9, y1, 'A')
plt.text(x2 + 0.4, y2, 'B')
plt.text(x3, y3 + 0.3, 'C')


#middle point of AB
m1x = (x1 + x2)/2
m1y = (y1 + y2)/2
plt.plot(m1x, m1y, marker = 'o', color = 'red')

plt.text(m1x, m1y-.5, 'M1')

#median from C to AB
plt.plot(
    (x3, m1x ),     
    (y3, m1y )
)

#middle point of AC
m2x = (x1 + x3) / 2
m2y = (y1 + y3) / 2

plt.plot(m2x, m2y, marker = 'o', color = 'black')
plt.text(m2x - 1.5, m2y, 'M2')

#middle point of B to AC
plt.plot(
    (x2, m2x),
    (y2, m2y)
)

#median from A to BC
m3x = (x2 + x3)/2
m3y = (y2 + y3)/2
plt.plot(m3x , m3y, marker= 'o' , color ='violet')
plt.text(m3x , m3y , 'M3')

plt.plot( 
    (x1, m3x), 
    (y1, m3y)
)

#centroid
mx = (x1 + x2 + x3)/3
my = (y1 + y2 + y3)/3
plt.plot( mx, my, marker = 'o',  color = 'orange')

plt.text(mx, my+.5 , 'M')

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()

Add points coordinates

In [12]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 2.3, y1 + 0.3, f'A({x1}, {y1})')
plt.text(x2 + 0.4, y2, f'B({x2}, {y2})')
plt.text(x3, y3 + 0.3, f'C({x3}, {y3})')


#middle point of AB
m1x = (x1 + x2)/2
m1y = (y1 + y2)/2
plt.plot(m1x, m1y, marker = 'o', color = 'red')

plt.text(m1x, m1y-.5, 'M1')

#median from C to AB
plt.plot(
    (x3, m1x ),     
    (y3, m1y )
)

#middle point of AC
m2x = (x1 + x3) / 2
m2y = (y1 + y3) / 2

plt.plot(m2x, m2y, marker = 'o', color = 'black')
plt.text(m2x - 1.5, m2y, 'M2')

#middle point of B to AC
plt.plot(
    (x2, m2x),
    (y2, m2y)
)

#median from A to BC
m3x = (x2 + x3)/2
m3y = (y2 + y3)/2
plt.plot(m3x , m3y, marker= 'o' , color ='violet')
plt.text(m3x , m3y , 'M3')

plt.plot( 
    (x1, m3x), 
    (y1, m3y)
)

#centeroid
mx = (x1 + x2 + x3)/3
my = (y1 + y2 + y3)/3
plt.plot( mx, my, marker = 'o',  color = 'orange')

plt.text(mx, my+.5 , 'M')

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()

Find the distance between AB, BC and CA

Now we will find the distance between AB, BC, CA. The distance between points (x1, y1) and (x2, y2) in the plane is given by:

In [13]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 2.3, y1 + 0.3, f'A({x1}, {y1})')
plt.text(x2 + 0.4, y2, f'B({x2}, {y2})')
plt.text(x3, y3 + 0.3, f'C({x3}, {y3})')


# AB distance 
ABx = (x1 + x2) / 2
ABy = (y1 + y2) / 2
plt.text(ABx, ABy - 0.6, f'AB({x2 - x1}, {y2 - y1})')

# BC distance
BCx = (x2 + x3) / 2
BCy = (y2 + y3) / 2
plt.text(BCx - 2, BCy + 0.4, f'BC({x3 - x2}, {y3 - y2})', rotation = -50)

# CA distance
CAx = (x3 + x1) / 2
CAy = (y3 + y1) / 2
plt.text(CAx - 1.3, CAy, f'AC({x1 - x3}, {y1 - y3})', rotation = 65)

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()
In [14]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 2.3, y1 + 0.3, f'A({x1}, {y1})')
plt.text(x2 + 0.4, y2, f'B({x2}, {y2})')
plt.text(x3, y3 + 0.3, f'C({x3}, {y3})')


# AB distance 
ABsqr = (x2 - x1) ** 2 + (y2 - y1) ** 2
print(ABsqr)

ABx = (x1 + x2) / 2
ABy = (y1 + y2) / 2
plt.text(ABx, ABy - 0.6, f'AB({x2 - x1}, {y2 - y1})')

# BC distance
BCsqr = (x3 - x2) ** 2 + (y3 - y2) ** 2
print(BCsqr)

BCx = (x2 + x3) / 2
BCy = (y2 + y3) / 2
plt.text(BCx - 2, BCy + 0.4, f'BC({x3 - x2}, {y3 - y2})', rotation = -50)

# CA distance
CAsqr = (x1 - x3) ** 2 + (y1 - y3) ** 2
print(CAsqr)

CAx = (x3 + x1) / 2
CAy = (y3 + y1) / 2
plt.text(CAx - 1.3, CAy, f'AC({x1 - x3}, {y1 - y3})', rotation = 65)

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()
225
164
89
In [15]:
x1, y1 = -5, 0
x2, y2 = 10, 0
x3, y3 = 0, 8

plt.plot(x1, y1, marker = 'o', markersize = 10)
plt.plot(x2, y2, marker = 'o', markersize = 10)
plt.plot(x3, y3, marker = 'o', markersize = 10)

# connecting all three points to make triangle
plt.plot( 
    (x1, x2, x3, x1),   
    (y1, y2, y3, y1)    
)

# name of the points 
plt.text(x1 - 2.3, y1 + 0.3, f'A({x1}, {y1})')
plt.text(x2 + 0.4, y2, f'B({x2}, {y2})')
plt.text(x3, y3 + 0.3, f'C({x3}, {y3})')


# AB distance 
ABsqr = (x2 - x1) ** 2 + (y2 - y1) ** 2
#print(ABsqr)
AB = ABsqr ** 0.5 

ABx = (x1 + x2) / 2
ABy = (y1 + y2) / 2
plt.text(ABx, ABy - 0.6, f'AB({x2 - x1}, {y2 - y1}) = {AB}')

# BC distance
BCsqr = (x3 - x2) ** 2 + (y3 - y2) ** 2
#print(BCsqr)
BC = round(   BCsqr ** 0.5, 2    )

BCx = (x2 + x3) / 2
BCy = (y2 + y3) / 2
plt.text(BCx - 2, BCy - 1, f'BC({x3 - x2}, {y3 - y2}) = {BC}', rotation = -50)

# CA distance
CAsqr = (x1 - x3) ** 2 + (y1 - y3) ** 2
#print(CAsqr)
CA = round(   CAsqr ** 0.5, 2    )

CAx = (x3 + x1) / 2
CAy = (y3 + y1) / 2
plt.text(CAx - 1.3, CAy, f'AC({x1 - x3}, {y1 - y3}) = {CA}', rotation = 65)

#set the xlim and ylim
plt.xlim(-10,15)
plt.ylim(-2,10)

plt.show()
In [ ]:
 
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