具有相同效果的选项是创建一个与坐标轴范围完全相同的白色矩形,这样坐标轴内的脊椎部分就会被矩形隐藏。这将需要使线宽两倍大,因为只能看到一半的线。
import matplotlib.pyplot as plt
# Make a dummy plot
fig, ax = plt.subplots()
ax.plot([0.01, 0, 1], [0.5, 0, 1], zorder=1)
ax.axis([0,1,0,1])
for axis in ['top','bottom','left','right']:
ax.spines[axis].set_linewidth(30)
ax.spines[axis].set_color("gold")
ax.spines[axis].set_zorder(0)
ax.add_patch(plt.Rectangle((0,0),1,1, color="w", transform=ax.transAxes))
ax.set_xlabel('X Axis', fontsize=16, fontweight='bold')
ax.set_ylabel('Y Axis', fontsize=16, fontweight='bold')
plt.show()
我在这里把刺变成黄色,这样它们就不会隐藏刻度和刻度标签。
另一种选择是调整Set matplotlib rectangle edge to outside of specified width? 的答案以创建一个矩形,该矩形严格包围图中的一个区域,如下所示:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.offsetbox import AnnotationBbox, AuxTransformBox
# Make a dummy plot
fig, ax = plt.subplots()
ax.plot([0.01, 0, 1], [0.5, 0, 1], zorder=1)
ax.axis([0,1,0,1])
linewidth=14
xy, w, h = (0, 0), 1, 1
r = Rectangle(xy, w, h, fc='none', ec='k', lw=1, transform=ax.transAxes)
offsetbox = AuxTransformBox(ax.transData)
offsetbox.add_artist(r)
ab = AnnotationBbox(offsetbox, (xy[0]+w/2.,xy[1]+w/2.),
boxcoords="data", pad=0.52,fontsize=linewidth,
bboxprops=dict(facecolor = "none", edgecolor='r',
lw = linewidth))
ab.set_zorder(0)
ax.add_artist(ab)
ax.set_xlabel('X Axis', fontsize=16, fontweight='bold')
ax.set_ylabel('Y Axis', fontsize=16, fontweight='bold')
plt.show()