开发者

matplotlib - zebra-stripe a figure's background color?

开发者 https://www.devze.com 2022-12-30 12:12 出处:网络
I\'m building a simple line chart with matplotlib, and I\'d like to zebra-stripe the background of the chart, so that each alternating row is colored differently. Is there a way to do this?

I'm building a simple line chart with matplotlib, and I'd like to zebra-stripe the background of the chart, so that each alternating row is colored differently. Is there a way to do this?

My chart already has gridding, and has major ticks only.

Edit: The code from my comment below, but more legible:

开发者_开发百科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'])


Here's a quick hack that uses a barchart (axes.barh) to simulate striping.

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

Produces:

matplotlib - zebra-stripe a figure's background color?

0

精彩评论

暂无评论...
验证码 换一张
取 消