How to draw a triangle using matplotlib.pyplot based on 3 dots (x,y) in 2D? How to draw a triangle using matplotlib.pyplot based on 3 dots (x,y) in 2D? python-3.x python-3.x

How to draw a triangle using matplotlib.pyplot based on 3 dots (x,y) in 2D?


A triangle is a polygon. You may use plt.Polygon to draw a polygon.

import numpy as np import matplotlib.pyplot as pltX = np.array([[1,1], [2,2.5], [3, 1], [8, 7.5], [7, 9], [9, 9]])Y = ['red', 'red', 'red', 'blue', 'blue', 'blue']plt.figure()plt.scatter(X[:, 0], X[:, 1], s = 170, color = Y[:])t1 = plt.Polygon(X[:3,:], color=Y[0])plt.gca().add_patch(t1)t2 = plt.Polygon(X[3:6,:], color=Y[3])plt.gca().add_patch(t2)plt.show()

enter image description here