![](/img/trans.png)
[英]How to plot fill_betweenx to fill the area between y1 and y2 with different scales using matplotlib.pyplot?
[英]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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.