matplotlib axis label format

前端 未结 2 1839
名媛妹妹
名媛妹妹 2020-12-02 15:39

I am having an issue with the format of the tick labels of an axis. I disabled the offset from the y_axis:

ax1.ticklabel_format(style = \'sci\', useOffset=Fa         


        
相关标签:
2条回答
  • 2020-12-02 16:27

    You should also specify axis and threshold limits:

    ax1.ticklabel_format(axis='y', style='sci', scilimits=(-2,2))
    

    This would use sci format on y axis when figures are out of the [0.01, 99] bounds.

    0 讨论(0)
  • 2020-12-02 16:29

    There are a number of ways to do this

    You could just tweak the power limits (doc)

    ax1.xaxis.get_major_formatter().set_powerlimits((0, 1))
    

    which set the powers where ScalerFormatter switches to scientific notation

    Or, you could use a FuncFormatter which gives you a good deal of control (but you can blow your foot off).

    from matplotlib import ticker
    
    scale_pow = 2
    def my_formatter_fun(x, p):
        return "%.2f" % (x * (10 ** scale_pow))
    ax1.get_xaxis().set_major_formatter(ticker.FuncFormatter(my_formatter_fun))
    ax1.set_xlabel('my label ' + '$10^{{{0:d}}}$'.format(scale_pow))
    

    FuncFormatter (doc) takes a 2 argument function that returns a string and uses that function to format the label. (Be aware, this will also change how the values are displayed in the corner of interactive figures). The second argument is for 'position' which is an argument handed when the formatter makes the labels. You can safely ignore it, but you must take it (other wise you will get errors from wrong number of arguments). This is a consequence of the unified API of all the formatters and using the formatter for displaying the location of the mouse in interactive.

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