python - Modify tick label text -


i want make modifications few selected tick labels in plot.

for example, if do:

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

the font size , orientation of tick label changed.

however, if try:

label.set_text('foo') 

the tick label not modified. if do:

print label.get_text() 

nothing printed.

here's more strangeness. when tried this:

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

only empty strings printed, plot contains ticks labeled '0.0', '0.5', '1.0', '1.5', , '2.0'.

caveat: unless ticklabels set string (as case in e.g. boxplot), not work version of matplotlib newer 1.1.0. if you're working current github master, won't work. i'm not sure problem yet... may unintended change, or may not be...

normally, you'd along these lines:

import matplotlib.pyplot plt  fig, ax = plt.subplots()  # need draw canvas, otherwise labels won't positioned ,  # won't have values yet. fig.canvas.draw()  labels = [item.get_text() item in ax.get_xticklabels()] labels[1] = 'testing'  ax.set_xticklabels(labels)  plt.show() 

enter image description here

to understand reason why need jump through many hoops, need understand bit more how matplotlib structured.

matplotlib deliberately avoids doing "static" positioning of ticks, etc, unless it's explicitly told to. assumption you'll want interact plot, , bounds of plot, ticks, ticklabels, etc dynamically changing.

therefore, can't set text of given tick label. default, it's re-set axis's locator , formatter every time plot drawn.

however, if locators , formatters set static (fixedlocator , fixedformatter, respectively), tick labels stay same.

this set_*ticklabels or ax.*axis.set_ticklabels does.

hopefully makes slighly more clear why changing individual tick label bit convoluted.

often, want annotate position. in case, annotate, instead.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -