5

Minimal working example of my code:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
from scipy.ndimage.filters import gaussian_filter
import numpy.random as nprnd

x = nprnd.randint(1000, size=5000)
y = nprnd.randint(1000, size=5000)

xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)
rang = [[xmin, xmax], [ymin, ymax]]
binsxy = [int((xmax - xmin) / 80), int((ymax - ymin) / 80)]

H, xedges, yedges = np.histogram2d(x, y, range=rang, bins=binsxy)
H_g = gaussian_filter(H, 2, mode='constant')

xline = 6.
yline = 4.

fig = plt.figure(figsize=(5, 5)) # create the top-level container
gs = gridspec.GridSpec(1, 1)  # create a GridSpec object
ax0 = plt.subplot(gs[0, 0])

# Set minor ticks
ax0.minorticks_on()
# Set grid
ax0.grid(b=True, which='major', color='k', linestyle='-', zorder=1)
ax0.grid(b=True, which='minor', color='k', linestyle='-', zorder=1)
# Add vertical and horizontal lines
plt.axvline(x=xline, linestyle='-', color='white', linewidth=4, zorder=2)
plt.axhline(y=yline, linestyle='-', color='white', linewidth=4, zorder=2)

plt.text(0.5, 0.91, 'Some text', transform = ax0.transAxes, \
bbox=dict(facecolor='white', alpha=1.0), fontsize=12)

plt.imshow(H_g.transpose(), origin='lower')

plt.show()

which returns this:

plot

As you can see, the grid is drawn on top of the axline & avline lines, even though I'm setting the zorder the other way around. How can I fix this?

I'm using Canopy v 1.0.1.1190.

Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • 1
    `grid` is disregarding the kwarg `zorder`. The grid is part of the `axis` objects for the x- and y- axis which has a `zorder` of 2.5. – tacaswell Jun 19 '13 at 20:25

1 Answers1

5

Your zorder=2 is too small. Increase it to zorder=3 and the axhline and axvline will be above the grid and below the label:

plt.axvline(x=xline, linestyle='-', color='white', linewidth=4, zorder=3)
plt.axhline(y=yline, linestyle='-', color='white', linewidth=4, zorder=3)

enter image description here

If you were to increase the zorder further, for example zorder=10, the lines would be on top of your some text-label.
More information on the default settings for zorder-values can be found here.

Charles
  • 104
  • 12
Schorsch
  • 7,761
  • 6
  • 39
  • 65
  • @Gabriel : Is this answer helpful to you? – Schorsch Jun 12 '13 at 12:19
  • @Scorsch definitely, thank you so much! I've already accepted this answer although I'm confused as to why I have to augment the `zorder` to 3 when 2 _should_ be enough since the grid is drawn with a `zorder=1`. In any case, thanks again. – Gabriel Jun 12 '13 at 13:08
  • @Gabriel : I agree. I haven't found a complete list for `zorder` defaults as of yet. If I do, I'll include it here. – Schorsch Jun 12 '13 at 14:37
  • 1
    @Gabriel I don't think there is a list of defaults, and the values are scattered throughout the code base. – tacaswell Jun 19 '13 at 20:18