0

I've got a library function which plots data into a pyplot figure containing several subplots. I just added grid lines to all of the subplots but they overlay the actual data but I would prefer them to be in the background. I've tried changing the order in which the plotting and ax.plot() ax.grid() commands are executed but that has no influence. Is there a way to force the grid into the background?

Related bonus question: I'm also using axhline to designate the x=0 line but it always assumes the grid colour even though it is being specified in a different one ...

The way the code currently works:

def plot_the_things(fig=None):
    # in case no figure is provided, make a new one,
    # otherwise add to the existing one
    plot_fig=fig if fig else plt.figure()

    #[...some calculations of data ...]

    plot_ax1 = plot_fig.add_subplot(3,3,1)
    plot_ax1.axhline(y=0, ls='-', color='0.5')
    plot_ax1.plot(self.diff_3[:,0],self.diff_3[:,1])
    # [...setting labels, adapt axes limits in case the new data needs wider ones..]
    plot_ax1.grid(b=True, which='major', axis='both', c='0.75', ls='-', linewidth=1)

    # this is repeated in similar fashion for the other axes -- there are 9 of
    # them, each plotting something different in a different axes

This function is called several times over. More precisely: It's actually part of a class. I have multiple instances of this class and call all of them, passing in the same figure object. Each instance then draws its own data, which works fine, and even the axhline() was shown properly (below the data!) but after I put in the command to add the grid, it always shows up on top of the data and covers the axhline, which is annoying.

... any way to fix this?

(I think I could and maybe should also move all the things that only need to run once to a place where they aren't repeatedly executed but time and mental resources are scant right now, so I went with the quickest way that worked... but I wouldn't expect this to change anything)

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Zak
  • 3,063
  • 3
  • 23
  • 30

1 Answers1

2

Use the zorder kwarg to your plot and axhline calls. The grid is plotted at zorder=2.5, so place the axhline and plot above this:

plot_ax1.axhline(y=0, ls='-', color='0.5', zorder=3)
plot_ax1.plot(self.diff_3[:,0],self.diff_3[:,1], zorder=4)
plot_ax1.grid(b=True, which='major', axis='both', c='0.75', ls='-', linewidth=1)

More info: here, and here.

farenorth
  • 10,165
  • 2
  • 39
  • 45
  • Also, this works: `plot_ax1.set_axisbelow(True)`. – farenorth Jul 03 '15 at 16:46
  • Worked! [This](https://stackoverflow.com/questions/1726391) already existing question is the same but your answer is more straightforward than the ones there – Zak Jul 03 '15 at 19:26
  • @Zak so encourage the answered to *put it there*, then everyone benefits. That's why we want to identify duplicates, to get all the useful information in one place. – jonrsharpe Jul 03 '15 at 20:26
  • excuse the noob question: How would I do this? I tried to add a pointer to my question (which you undid), but I've no idea how else I'd make this show up for people looking up the other question -- especially since the text is specific to my question... I could answer the other question using farenorth's method but that seems not right – Zak Jul 04 '15 at 17:45
  • I already made a comment on an answer to the original question that clarifies what I thought was the important piece of this answer. – farenorth Jul 05 '15 at 05:58