我不知道如何绘制以下内容。我的数据文件包含以下结构中许多三角形的角点:
1 0
0 1
1 1
0.1 1
0.2 2
0.3 3
我想在一张图片中绘制它们,并以不同方式为每个三角形的表面着色。我该怎么做?
感谢您的帮助:)
答案 0 :(得分:1)
快速查看artist demo
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
fig = plt.figure(figsize=(5,5))
ax = plt.axes([0,0,1,1])
triangle1 = mpatches.Polygon(np.array([[0,1],[1,0],[1,1]]), fc="blue")
triangle2 = mpatches.Polygon(np.array([[-0.1,-1],[-2,-2],[-2,-1]]), fc="red")
ax.add_artist(triangle1)
ax.add_artist(triangle2)
ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
plt.show()
答案 1 :(得分:0)
使用您发布的数据:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
import itertools
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
with open('data') as f:
for points in zip(*[itertools.ifilter(lambda line: line.strip(),f)]*3):
points=([tuple(map(float,p.strip().split())) for p in points])
ax.add_patch(patches.Polygon(points))
ax.autoscale_view()
plt.show()