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

前端 未结 1 2021
醉酒成梦
醉酒成梦 2020-12-31 23:14

I would like to draw a triangle using python3 module matplotlib.

import numpy as np 
import matplotlib.pyplot as plt

X_train = np.array([[1,1], [2,2.5], [3,         


        
相关标签:
1条回答
  • 2020-12-31 23:55

    A triangle is a polygon. You may use plt.Polygon to draw a 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()
    

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题