如果游戏中有很多行,使用LineCollection可能会很方便。与其他答案类似,但价格较便宜:
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectiondef 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 cln = 10y1 = np.random.random(n)y2 = np.random.random(n) + 1x = 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()



