如何使用 matplotlib.pyplot 基于 3 个点 (x,y) 在 2D 中绘制三角形?

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

我想用 python3 模块 matplotlib 画一个三角形。

import numpy as np 
import matplotlib.pyplot as plt

X_train = np.array([[1,1], [2,2.5], [3, 1], [8, 7.5], [7, 9], [9, 9]])
Y_train = ['red', 'red', 'red', 'blue', 'blue', 'blue']

plt.figure()
plt.scatter(X_train[:, 0], X_train[:, 1], s = 170, color = Y_train[:])
plt.show()

它显示 6 个点,但它们在 2 个地方分开分组。 (颜色有助于看清楚)

有 2 组 3 个点。我希望每组(3 个点)都统一在三角形中。

这怎么可能实现?如何使用 matplotlib 基于 3 个点构建三角形?

如有任何建议,我们将不胜感激;)

三角形是多边形。您可以使用 plt.Polygon 绘制多边形。

import numpy as np 
import matplotlib.pyplot as plt

X = 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()