Modify tick label text

前端 未结 10 1328
遥遥无期
遥遥无期 2020-11-22 07:13

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         


        
10条回答
  •  旧时难觅i
    2020-11-22 08:09

    In newer versions of matplotlib, if you do not set the tick labels with a bunch of str values, they are '' by default (and when the plot is draw the labels are simply the ticks values). Knowing that, to get your desired output would require something like this:

    >>> from pylab import *
    >>> axes = figure().add_subplot(111)
    >>> a=axes.get_xticks().tolist()
    >>> a[1]='change'
    >>> axes.set_xticklabels(a)
    [, , 
    , , 
    , ]
    >>> plt.show()
    

    and the result: enter image description here

    and now if you check the _xticklabels, they are no longer a bunch of ''.

    >>> [item.get_text() for item in axes.get_xticklabels()]
    ['0.0', 'change', '1.0', '1.5', '2.0']
    

    It works in the versions from 1.1.1rc1 to the current version 2.0.

提交回复
热议问题