jbr*_*eed 1 python charts matplotlib
我正在使用 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'])
Run Code Online (Sandbox Code Playgroud)
这是一个使用条形图 (axes.barh) 来模拟条纹的快速技巧。
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()
Run Code Online (Sandbox Code Playgroud)
产生:
| 归档时间: |
|
| 查看次数: |
3341 次 |
| 最近记录: |