gpt4 book ai didi

python-2.7 - Matplotlib - 网格总是在 ax-h/v-lines 前面

转载 作者:行者123 更新时间:2023-12-04 20:11:14 25 4
gpt4 key购买 nike

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

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

返回这个:

plot

如您所见,绘制了网格 顶部 axline & avline行,即使我正在设置 zorder另一种方式。我怎样才能解决这个问题?

我正在使用 Canopy v 1.0.1.1190。

最佳答案

您的 zorder=2太小。
将其增加到 zorder=3axhlineaxvline将在网格上方和标签下方:

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
如果要增加 zorder进一步,例如 zorder=10 ,线条将在您的 some text 之上-标签。
有关 zorder 默认设置的更多信息-值可以找到 here .

关于python-2.7 - Matplotlib - 网格总是在 ax-h/v-lines 前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17029017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com