5

我的代码的最小工作示例:

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

它返回这个:

阴谋

正如你所看到的,网格是在&线的顶部绘制的,即使我设置的是相反的方式。我怎样才能解决这个问题?axlineavlinezorder

我正在使用Canopy v 1.0.1.1190

4

1 回答 1

5

zorder=2的太小了。将其增加到zorder=3和将位于网格上方和标签下方axhlineaxvline

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

在此处输入图像描述

zorder例如,如果您要进一步增加,则这些行将位于您的-labelzorder=10之上。有关-values 的默认设置的更多信息,请参见此处some text
zorder

于 2013-06-11T23:30:07.613 回答