轴脊#

此演示比较了:

  • 普通的 Axes,四边都有轴脊;

  • 仅在左侧和底部有轴脊的 Axes;

  • 使用自定义边界来限制轴脊范围的 Axes.

每个 axes.Axes 都有一个 Spine 对象列表,可以通过容器 ax.spines 进行访问.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 100)
y = 2 * np.sin(x)

# Constrained layout makes sure the labels don't overlap the Axes.
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, layout='constrained')

ax0.plot(x, y)
ax0.set_title('normal spines')

ax1.plot(x, y)
ax1.set_title('bottom-left spines')

# Hide the right and top spines
ax1.spines.right.set_visible(False)
ax1.spines.top.set_visible(False)

ax2.plot(x, y)
ax2.set_title('spines with bounds limited to data range')

# Only draw spines for the data range, not in the margins
ax2.spines.bottom.set_bounds(x.min(), x.max())
ax2.spines.left.set_bounds(y.min(), y.max())
# Hide the right and top spines
ax2.spines.right.set_visible(False)
ax2.spines.top.set_visible(False)

plt.show()
normal spines, bottom-left spines, spines with bounds limited to data range

参考

以下函数,方法,类和模块的用法在本例中显示:

  • matplotlib.artist.Artist.set_visible

  • matplotlib.spines.Spine.set_bounds

Gallery generated by Sphinx-Gallery