In matplotlib, is there a way to set the grid lines below bars / lines / patches while keeping ticklabels higher?

Matplotlib: draws grid lines behind other elements of the chart , but nothing worked for me.

I have the following graph where I want to hide the grid lines under the red line, keeping the labels above the red line:

import numpy as np import matplotlib.pyplot as plt #plot r = np.arange(0, 3.0, 0.01) theta = 2 * np.pi * r ax = plt.subplot(111, polar=True) ax.plot(theta, r, color='r', linewidth=20) ax.set_rmax(2.0) ax.grid(True, lw=2) #set labels label_pos = np.linspace(0.0, 2 * np.pi, 6, endpoint=False) ax.set_xticks(label_pos) label_cols = ['Label ' + str(num) for num in np.arange(6)] ax.set_xticklabels(label_cols, size=24) 

enter image description here

I can get the red line on top of ax.set_axisbelow(True) .

enter image description here

But I can’t find a way to keep the red line on top of the grid lines, keeping the labels on top of the red line. When adding zorder=-1 to the plot command, put the red line at the bottom, even if I add ax.set_axisbelow(True) . ax.set_zorder(-1)) does not work yet.

How can I get the grid lines at the bottom (the lowest zorder), followed by the red line, and then the labels above the red line?

+2
python matplotlib z-order gridlines
Apr 08 '15 at 18:22
source share
1 answer

You can always build the grid manually:

 import numpy as np import matplotlib.pyplot as plt #plot r = np.arange(0, 3.0, 0.01) theta = 2 * np.pi * r rmax = 2.0 n_th = 6 th_pos = np.linspace(0.0, 2 * np.pi, n_th, endpoint=False) n_r = 5 r_pos = np.linspace(0, rmax, n_r) ax = plt.subplot(111, polar=True) ## Plot the grid for pos in th_pos: ax.plot([th_pos]*2, [0, rmax], 'k:', lw=2) for pos in r_pos[1:-1]: x = np.linspace(0, 2*np.pi, 50) y = np.zeros(50)+pos ax.plot(x, y, 'k:', lw=2) ## Plot your data ax.plot(theta, r, color='r', linewidth=20) ax.set_rmax(rmax) ax.grid(False) #set ticks and labels ax.set_xticks(th_pos) label_cols = ['Label ' + str(num) for num in np.arange(n_th)] ax.set_xticklabels(label_cols, size=24) ax.set_yticks(r_pos[1:]) plt.show() 

enter image description here

+1
Apr 08 '15 at 20:13
source share



All Articles