我正在使用 matplotlib 构建一个简单的折线图,我想对图表的背景进行斑马条纹处理,以便每个交替行的颜色都不同。有办法做到这一点吗?
我的图表已经有了网格,而且只有主要的刻度线。
编辑:我下面评论中的代码,但更清晰:
yTicks = ax.get_yticks()[:-1]
xTicks = ax.get_xticks()
ax.barh(yTicks, [max(xTicks)-min(xTicks)] * len(yTicks),
height=(yTicks[1]-yTicks[0]), left=min(xTicks), color=['w','#F0FFFF'])
最佳答案
这是一个使用条形图 (axes.barh) 模拟 strip 化的快速技巧。
import matplotlib.pyplot as plt
# initial plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3,4,5])
yTickPos,_ = plt.yticks()
yTickPos = yTickPos[:-1] #slice off the last as it is the top of the plot
# create bars at yTickPos that are the length of our greatest xtick and have a height equal to our tick spacing
ax.barh(yTickPos, [max(plt.xticks()[0])] * len(yTickPos), height=(yTickPos[1]-yTickPos[0]), color=['g','w'])
plt.show()
产生:
关于python - matplotlib - 斑马条纹图形的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2815455/