首页 > 解决方案 > matlibplot 基于矩阵的绘图线

问题描述

matplotlib 和 numpy 的新手。

我有一个代表 L 形的矩阵:

import numpy as np
L = np.array([[1, 1, 1.5, 1.5, 2, 2], [2, 4, 4, 2.5, 2.5, 2]])

L形

如何在 matplotlib 中绘制它?

标签: numpymatplotlib

解决方案


让我们尝试一些高级索引:

plt.plot(*L[:, range(-1,L.shape[1])])
plt.xlim(0, 5)
plt.ylim(0, 5)
plt.show()

或者可能更自然地使用Patch.Polygon

from matplotlib.patches import Polygon

fig, ax = plt.subplots()
ax.add_patch(Polygon(L.T, facecolor='None', edgecolor='C0'))
ax.set_xlim(0,5)
ax.set_ylim(0,5)
plt.show()

输出:

在此处输入图像描述


推荐阅读