用水平线连接点

硬化的

我们的目标是,以填补两个阵列之间的空间y1y2,类似于matplotlib的fill_between但是我不想用多边形(例如用hatch='|'填充空间,而是只在两个数组的数据点之间绘制垂直线。

import matplotlib.pyplot as plt
import numpy as np

n = 10
y1 = np.random.random(n)
y2 = np.random.random(n) + 1
x1 = np.arange(n)
ax.fill_between(x1, y1, y2, facecolor='w', hatch='|')

matplotlib fill_between

认真的重要性

如果游戏中有很多行,使用LineCollection可能会很方便。与其他答案类似,但价格较便宜:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

def draw_lines_between(*, x1=None, x2=None, y1, y2, ax=None, **kwargs):
    ax = ax or plt.gca()
    x1 = x1 if x1 is not None else np.arange(len(y1))
    x2 = x2 if x2 is not None else x1

    cl = LineCollection(np.stack((np.c_[x1, x2], np.c_[y1, y2]), axis=2), **kwargs)
    ax.add_collection(cl)
    return cl

n = 10
y1 = np.random.random(n)
y2 = np.random.random(n) + 1
x = np.arange(n)
color_list = [str(x) for x in np.round(np.linspace(0., 0.8, n), 2)]

fig, ax = plt.subplots()
ax.plot(x, y1, 'r')
ax.plot(x, y2, 'b')
draw_lines_between(ax=ax, x1=x, y1=y1, y2=y2, colors=color_list)

plt.show()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章