【问题标题】:how to set the properties of axis tick labels when using customized tick locator and formatter? [duplicate]使用自定义刻度定位器和格式化程序时如何设置轴刻度标签的属性? [复制]
【发布时间】:2018-03-20 07:12:59
【问题描述】:

在动态设置刻度的情况下(如以下示例),如何更改刻度标签properties(如sizerotation 等)?我试过ax.set_xticklabels(ax.get_xticklabels(), rotation=90, size=7, ha='center'),它不起作用。实际上,ax.get_xticklabels() 甚至没有返回 5 中设置的正确数量的刻度标签 MaxNLocator - 这是一个错误吗?

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MaxNLocator
fig, ax = plt.subplots()
xs = range(26)
ys = range(26)
labels = list('abcdefghijklmnopqrstuvwxyz')


def format_fn(tick_val, tick_pos):
    if int(tick_val) in xs:
        return labels[int(tick_val)]
    else:
        return ''


ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(5, integer=True))
ax.set_xticklabels(ax.get_xticklabels(), rotation=90, size=7, ha='center')
ax.plot(xs, ys)

ax.get_xticklabels() 获取空刻度标签:

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MaxNLocator
fig, ax = plt.subplots()
xs = range(26)
ys = range(26)
labels = list('abcdefghijklmnopqrstuvwxyz')


def format_fn(tick_val, tick_pos):
    if int(tick_val) in xs:
        return labels[int(tick_val)]
    else:
        return ''


ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(5, integer=True))
ax.set_xticklabels(labels, rotation=90, size=7, ha='center')
ax.plot(xs, ys)

这会将标签偏移 1。它应该以 a 开头,而不是 b

在答案hereplt.xticks 可以做的工作。但是,我想知道是否有任何方法可以使用面向对象的接口?因为我不想用plt.xticks 全局设置这些属性。

【问题讨论】:

  • 面向对象的方法是for t in ax.get_xticklabels(): t.set_rotation(90)。由于这个答案已经在this question 中给出,我实际上认为它是重复的。也许您想编辑问题以说明在多大程度上不能解决问题?!
  • @ImportanceOfBeingErnest 它绝对不是重复的,正如本页多个地方所指出的那样。如果没有设置刻度标签,ax.get_xticklabels() 将不起作用。您引用问题的替代解决方案确实有效..

标签: python matplotlib


【解决方案1】:

编辑:回想起来,找到了非常合乎逻辑的解决方案。

ax.set_xticklabels(ax.get_xticklabels(), rotation=90, size=7, ha='center') 旨在将标签 a g m s y 旋转 90 度并使它们稍微小一点。您从 ax.get_xticklabels() 获得的两个额外刻度标签是绘图轴限制处的空刻度 ''。它们正是你告诉你的情节与MaxNLocator 做的事情,因为这会选择N 间隔,而不是N 滴答声。

只有当脚本完成并且我随后调用ax 时,我才能真正访问刻度标签。这表明在绘图时,未设置刻度标签,仅设置了格式化程序和定位器,稍后会从中生成刻度。这意味着ax.get_xticklabels() 在指定位置返回一个空列表。

因此,要通过对 ax 的引用进行这项工作,您必须手动设置刻度。

import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from matplotlib.ticker import FuncFormatter, MaxNLocator
from matplotlib.text import Text
fig, ax = plt.subplots()
xs = range(26)
ys = range(26)
labels = list('abcdefghijklmnopqrstuvwxyz')

def format_fn(tick_val, tick_pos):
    if int(tick_val) in xs:
        return labels[int(tick_val)]
    else:
        return ''

ax.set_xticklabels(labels, rotation=90, size=7, ha='center')
ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(5, integer=True))

ax.plot(xs, ys)
plt.show()

感谢 ImportanceOfBeingErnest,因为他找到了解决这个问题的有效解决方案,甚至在它成为问题之前:

fig.autofmt_xdate(bottom=0.1, rotation=90, ha='center')

也可以,因为图形对象可以在绘图时访问,从而无需“手动”设置刻度标签。但是,请注意,在缩放时这可能会与 set_major_formatterset_major_locator 函数产生令人讨厌的交互。

【讨论】:

  • 很高兴了解两个轴限制处的空刻度。 ax.set_xticklabels 绝对不能正常工作。如果可能的话,我会避免像上一个示例那样明确输入所有刻度标签。
  • 答案已更新,之前的所有错误都是对尚未设置的 ax.get_xticklabels() 的调用,而不是对实际标签的调用。
【解决方案2】:

您可以在调用plot 之前使用plt.xticks(rotation=90)(在交互模式下工作),或使用仅在非交互模式下工作的plt.setp(ax.xaxis.get_majorticklabels(), rotation=90)

this post for more info

【讨论】:

  • 谢谢。是否有任何面向对象的接口可以做到这一点?
猜你喜欢
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 2017-03-16
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
相关资源
最近更新 更多