matplotlib轴标签格式 [英] matplotlib axis label format

查看:81
本文介绍了matplotlib轴标签格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对轴的刻度标签格式有疑问.我禁用了y_axis的偏移量:

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=False)

并尝试将其设置为科学格式,但我得到的只是:

and tried to put it a scientific format but all I get is:

0.00355872

但是我期望这样:

3.55872...E-2

或类似的

我真正想要的是这样的东西:

what I really want is something like:

3.55872... (on the tick label)
x 10^2  (or something similar - on the axis label)

我可以尝试将标签设置为静态,但是最后我将拥有数十或数百个具有不同值的图,因此需要动态设置.

I could try to set the labels as static,, but in the end I will have a few tens or hundreds of plots with different values, so it needs to be set dynamically.

一种替代方法是将y_axis偏移量作为标签放置,但是我也不知道如何执行此操作.

An alternative would be to place the y_axis offset as the label, but I also have no clue on how to do this.

推荐答案

有很多方法可以做到这一点

There are a number of ways to do this

您可以调整功率限制(doc)

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

设置ScalerFormatter转换为科学计数法的能力

which set the powers where ScalerFormatter switches to scientific notation

或者,您可以使用FuncFormatter来提供很多控制权(但是您可以放开脚).

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)采用2参数函数返回一个字符串,并使用该函数格式化标签. (请注意,这还将更改在交互式图形的一角显示值的方式).第二个参数用于位置",这是格式化程序制作标签时使用的参数.您可以放心地忽略它,但是必须使用它(否则,您将从错误数量的参数中得到错误).这是所有格式器的统一API的结果,并且使用格式器以交互方式显示鼠标的位置.

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.

这篇关于matplotlib轴标签格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆