Modify tick label text

2019-01-01 06:33发布

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

For example, if I do:

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.

However, if try:

label.set_text('Foo')

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

print label.get_text()

nothing is printed.

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()

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

7条回答
何处买醉
2楼-- · 2019-01-01 07:27

you can do:

for k in ax.get_xmajorticklabels():
    if some-condition:
        k.set_color(any_colour_you_like)

draw()
查看更多
登录 后发表回答