2013-06-10 23 views
5

Minimal dụ làm việc của mã của tôi:Matplotlib - Lưới luôn luôn ở phía trước rìu-h/v-dòng

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

trả về này:

plot

Như bạn có thể thấy, lưới được vẽ trên đầu trang trong số axline & avline dòng, mặc dù tôi đang đặt zorder theo cách khác. Làm thế nào tôi có thể sửa lỗi này?

Tôi đang sử dụng Canopy v 1.0.1.1190.

+1

'lưới' sẽ bỏ qua kwarg' zorder'. Lưới là một phần của các đối tượng 'trục' cho trục x và trục y có một' thứ tự' là 2,5. – tacaswell

Trả lời

4

zorder=2 của bạn quá nhỏ.
Tăng nó để zorder=3axlineavline sẽ vượt lưới và dưới nhãn:

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

Nếu bạn đã tăng zorder hơn nữa, ví dụ zorder=10, các dòng sẽ là trên đầu trang của nhãn some text.
Thông tin thêm về cài đặt mặc định cho zorder -giá trị có thể được tìm thấy here.

+0

@Gabriel: Câu trả lời này có hữu ích cho bạn không? – Schorsch

+0

@Scorsch chắc chắn, cảm ơn bạn rất nhiều! Tôi đã chấp nhận câu trả lời này mặc dù tôi đang bối rối là tại sao tôi phải tăng thêm 'zorder' thành 3 khi 2 _should_ là đủ vì lưới được vẽ bằng' zorder = 1'. Trong mọi trường hợp, cảm ơn một lần nữa. – Gabriel

+0

@Gabriel: Tôi đồng ý. Tôi chưa tìm thấy danh sách đầy đủ cho các giá trị mặc định 'zorder'. Nếu tôi làm, tôi sẽ đưa nó vào đây. – Schorsch

Các vấn đề liên quan