修改刻度标签文本 [英] Modify tick label text

查看:63
本文介绍了修改刻度标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对图中的几个选定的刻度线标签进行一些修改.

I want to make some modifications to a few selected tick labels in a plot.

例如,如果我这样做:

label = axes.yaxis.get_major_ticks()[2].label
label.set_fontsize(size)
label.set_rotation('vertical')

更改了字体大小和刻度标签的方向.

the font size and the orientation of the tick label is changed.

但是,如果尝试:

label.set_text('Foo')

对勾标签进行了修改.另外,如果我这样做:

the tick label is not modified. Also if I do:

print label.get_text()

什么都没打印.

这里有些陌生.当我尝试这样做时:

Here's some more strangeness. When I tried this:

 from pylab import *
 axes = figure().add_subplot(111)
 t = arange(0.0, 2.0, 0.01)
 s = sin(2*pi*t)
 axes.plot(t, s)
 for ticklabel in axes.get_xticklabels():
     print ticklabel.get_text()

仅打印空字符串,但图中包含标记为"0.0","0.5","1.0","1.5"和"2.0"的刻度.

Only empty strings are printed, but the plot contains ticks labeled as '0.0', '0.5', '1.0', '1.5', and '2.0'.

推荐答案

注意:除非将ticklabel设置为字符串(例如,在boxplot中通常如此),否则这不适用于任何较新的matplotlib版本比1.1.0.如果您正在使用当前的github主机,则此操作将无效.我不确定问题是什么...可能是意外更改,也可能不是...

Caveat: Unless the ticklabels are already set to a string (as is usually the case in e.g. a boxplot), this will not work with any version of matplotlib newer than 1.1.0. If you're working from the current github master, this won't work. I'm not sure what the problem is yet... It may be an unintended change, or it may not be...

通常,您会按照以下方式进行操作:

Normally, you'd do something along these lines:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# We need to draw the canvas, otherwise the labels won't be positioned and 
# won't have values yet.
fig.canvas.draw()

labels = [item.get_text() for item in ax.get_xticklabels()]
labels[1] = 'Testing'

ax.set_xticklabels(labels)

plt.show()

要了解需要跳过这么多圈的原因,您需要进一步了解matplotlib的结构.

To understand the reason why you need to jump through so many hoops, you need to understand a bit more about how matplotlib is structured.

Matplotlib故意避免对刻度等进行静态"定位,除非明确告知.假设您将要与图进行交互,因此图的边界,刻度线,刻度标签等将动态变化.

Matplotlib deliberately avoids doing "static" positioning of ticks, etc, unless it's explicitly told to. The assumption is that you'll want to interact with the plot, and so the bounds of the plot, ticks, ticklabels, etc will be dynamically changing.

因此,您不能只设置给定刻度标签的文本.默认情况下,每次绘制图形时,都会通过轴的定位器"和格式化程序"对其进行重置.

Therefore, you can't just set the text of a given tick label. By default, it's re-set by the axis's Locator and Formatter every time the plot is drawn.

但是,如果将定位器"和格式器"设置为静态(分别为FixedLocatorFixedFormatter),则刻度标签将保持不变.

However, if the Locators and Formatters are set to be static (FixedLocator and FixedFormatter, respectively), then the tick labels stay the same.

这是set_*ticklabelsax.*axis.set_ticklabels所做的.

希望这使我们更清楚地知道为什么更改单个刻度标签有些麻烦.

Hopefully that makes it slighly more clear as to why changing an individual tick label is a bit convoluted.

通常,您实际要做的只是注释某个位置.在这种情况下,请查看annotate.

Often, what you actually want to do is just annotate a certain position. In that case, look into annotate, instead.

这篇关于修改刻度标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆