使用matplotlib的多种颜色的色轴书脊

使用matplotlib的多种颜色的色轴书脊,第1张

使用matplotlib的多种颜色的色轴书脊

您可以使用

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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5654994.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存