matplotlib: change yaxis tick labels

前端 未结 2 1163
温柔的废话
温柔的废话 2020-12-10 06:26

For each tick label on the y axis, I would like to change: label -> 2^label

I am plotting log-log data (base 2), but I would like the labels to show

相关标签:
2条回答
  • 2020-12-10 06:56

    As per http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_yticks

    a = plt.gca()
    a. set_yticks(list_of_labels)

    0 讨论(0)
  • 2020-12-10 07:05

    If you want to do this in a general case you can use FuncFormatter (see : matplotlib axis label format, imshow: labels as any arbitrary function of the image indices. Matplotlib set_major_formatter AttributeError)

    In you case the following should work:

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    
    def mjrFormatter(x, pos):
        return "$2^{{{0}}}$".format(x)
    
    def mjrFormatter_no_TeX(x, pos):
        return "2^{0}".format(x)
    
    ax = plt.gca()
    ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(mjrFormatter))
    plt.draw()
    

    The absured {} escaping is a consequence of the new-style string frommating

    0 讨论(0)
提交回复
热议问题