views:

195

answers:

1

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'])
+1  A: 

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:

alt text

Mark
Thank you! I had a bit of trouble getting it to work at first, because my x-axis contains dates, so I altered it thusly: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'])(notice that there's a new left= arg, and that each individual bar width is max(xticks) - min(xticks), rather than just max(xticks).)
jawonlee