使用matplotlib的多种颜色的色轴书脊 [英] Color axis spine with multiple colors using matplotlib
本文介绍了使用matplotlib的多种颜色的色轴书脊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在python中使用matplotlib为轴脊柱使用多种颜色上色?
Is it possible to color axis spine with multiple colors using matplotlib in python?
所需的输出样式:
推荐答案
您可以使用LineCollection
创建彩色线条.然后,您可以使用xaxis-transform将其固定在xaxis上,而与y-limits无关.将实际的书脊设置为不可见并关闭clip_on
可使LineCollection看起来像轴书脊.
You can use a LineCollection
to create a multicolored line. You can then use the xaxis-transform to keep it fixed to the xaxis, independent of the y-limits. Setting the actual spine invisible and turning clip_on
off makes the LineCollection look like the axis spine.
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import numpy as np
fig, 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()
这篇关于使用matplotlib的多种颜色的色轴书脊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文