【问题标题】:How to set y-axis tick label in matplot lib如何在 matplotlib 中设置 y 轴刻度标签
【发布时间】:2020-06-02 03:27:47
【问题描述】:

我的 Y 轴刻度标签显示出奇怪的行为。 默认情况下,Y 轴具有从文件读取的最大限制,但我无法增加标签大小。 所以我插入了

ax2.tick_params(axis="y", labelsize=28)

插入此行后,Y 轴变为 15,次刻度标签位于 2.5 的间隔处并显示为浮点数(例如,1 显示为 1.0)

我尝试使用下面的方法将 Y 轴标签设为整数,但不能。

ax1.set_yticklabels(tick_labels.astype(int))

我想保留我的 Y 轴标签默认值,但想增加标签大小。

我遇到错误

    ax1.set_yticklabels(tick_labels.astype(int))
NameError: name 'tick_labels' is not defined

【问题讨论】:

  • 什么是ax2?你有两个 y 轴吗?
  • 我有一个 (221) 到 (224) 大小的图,这里 ax2 代表 222 的图。

标签: matplotlib


【解决方案1】:

您可以按照this answer尝试以下解决方案

import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

fig, ax = plt.subplots()

# your code here

# For minor ticks
ax.yaxis.set_minor_locator(MaxNLocator(integer=True))

# For major ticks
ax.yaxis.set_major_locator(MaxNLocator(integer=True))

【讨论】:

  • 是的,它对我有用。 Y轴的间隔如何控制?