Matplotlib 中重叠的 Y 轴刻度标签和 X 轴刻度标签


为了减少在 matplotlib 中重叠的可能性 刻度标签,我们可以采取以下步骤 -

  • 使用 numpy 创建 数据点。

  • 在索引 1 处(nrows=1 和 ncols=2)向当前图形添加子图。

  • 边距设置为 0。

  • 绘制 数据点,并为此子图添加一个标题,即“重叠”。

  • 在索引 2 处(nrows=1 和 ncols=2)向当前图形添加子图。

  • 边距设置为 0。

  • 绘制 x 和 y 数据点,并为此子图添加一个标题,即“不重叠”。

  • MaxNLocator 和 prune ="lower" 的目标是最小的刻度将被移除。

  • 要显示图形,请使用 show() 方法。

范例

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
xs = np.linspace(0, 5, 10)
ys = np.linspace(0, 5, 10)
plt.subplot(121)
plt.margins(x=0, y=0)
plt.plot(xs, ys)
plt.title("Overlapping")
plt.subplot(122)
plt.margins(x=0, y=0)
plt.plot(xs, ys)
plt.title("Non overlapping")
plt.gca().xaxis.set_major_locator(MaxNLocator(prune='lower'))
plt.gca().yaxis.set_major_locator(MaxNLocator(prune='lower'))
plt.show()

输出

更新于: 06-May-2021

3K+ 浏览量

开启你的 职业生涯

完成课程,获取认证

开始学习
广告
© . All rights reserved.