如何使用基于 2D 中的 3 个点 (x,y) 的 matplotlib.pyplot 绘制三角形? [英] How to draw a triangle using matplotlib.pyplot based on 3 dots (x,y) in 2D?

查看:53
本文介绍了如何使用基于 2D 中的 3 个点 (x,y) 的 matplotlib.pyplot 绘制三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

将 numpy 导入为 np导入 matplotlib.pyplot 作为 pltX_train = np.array([[1,1], [2,2.5], [3, 1], [8, 7.5], [7, 9], [9, 9]])Y_train = ['红色','红色','红色','蓝色','蓝色','蓝色']plt.figure()plt.scatter(X_train[:, 0], X_train[:, 1], s = 170, color = Y_train[:])plt.show()

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

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

如何实现这一点?如何使用 matplotlib 基于 3 个点构建三角形?

感谢任何建议;)

解决方案

三角形是多边形.您可以使用

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], [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()

It displays 6 dots but they are grouped separately in 2 places. (color helps to see it clearly)

There are 2 sets of 3 dots. I want each set(3 dots) be united in the triangle.

How is it possible to implement this? How to build a triangle based on 3 dots using matplotlib?

Any suggestions are appreciated ;)

解决方案

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

这篇关于如何使用基于 2D 中的 3 个点 (x,y) 的 matplotlib.pyplot 绘制三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆