自动设置刻度位置#

设置刻度自动放置的行为。

默认情况下,Matplotlib 会选择刻度数量和刻度位置,以确保坐标轴上有合理数量的刻度,并且这些刻度位于“规整”的数字上。

因此,绘图边缘可能没有刻度。

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

fig, ax = plt.subplots()
dots = np.linspace(0.3, 1.2, 10)
X, Y = np.meshgrid(dots, dots)
x, y = X.ravel(), Y.ravel()
ax.scatter(x, y, c=x+y)
plt.show()
auto ticks

如果您想保留规整的刻度,同时又希望在边缘也有刻度,可以将 rcParams["axes.autolimit_mode"](默认值:'data')切换到 'round_numbers'。这会将坐标轴限制扩展到下一个规整数字。

plt.rcParams['axes.autolimit_mode'] = 'round_numbers'

# Note: The limits are calculated at draw-time. Therefore, when using
# :rc:`axes.autolimit_mode` in a context manager, it is important that
# the ``show()`` command is within the context.

fig, ax = plt.subplots()
ax.scatter(x, y, c=x+y)
plt.show()
auto ticks

如果您使用 Axes.set_xmargin / Axes.set_ymargin 在数据周围设置了额外的边距,round_numbers autolimit_mode 仍然会被遵守。

auto ticks

脚本总运行时间: (0 分钟 2.248 秒)

由 Sphinx-Gallery 生成的画廊