MatPlotLib颜色三角形

时间:2011-09-16 23:23:31

标签: python matplotlib

我不知道如何绘制以下内容。我的数据文件包含以下结构中许多三角形的角点:

1 0
0 1
1 1

0.1 1
0.2 2
0.3 3

我想在一张图片中绘制它们,并以不同方式为每个三角形的表面着色。我该怎么做?

感谢您的帮助:)

2 个答案:

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

triangle

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

enter image description here