您可以使用
LineCollection创建彩色线条。然后,您可以使用xaxis-
transform将其固定在xaxis上,而与y-
limits无关。将实际的书脊设置为不可见并
clip_on关闭会使LineCollection看起来像轴书脊。
import matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectionimport numpy as npfig, ax = plt.subplots()colors=["b","r","lightgreen","gold"]x=[0,.25,.5,.75,1]y=[0,0,0,0,0]points = np.array([x, y]).T.reshape(-1, 1, 2)segments = np.concatenate([points[:-1], points[1:]], axis=1)lc = LineCollection(segments,colors=colors, linewidth=2, transform=ax.get_xaxis_transform(), clip_on=False )ax.add_collection(lc)ax.spines["bottom"].set_visible(False)ax.set_xticks(x)plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)