Python matplotlib 轴标签格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14775040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:24:06  来源:igfitidea点击:

matplotlib axis label format

pythonmatplotlib

提问by jorgehumberto

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

我遇到了轴刻度标签格式的问题。我禁用了 y_axis 的偏移量:

ax1.ticklabel_format(style = 'sci', useOffset=False)

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

并试图把它变成一种科学的格式,但我得到的只是:

0.00355872

but I expected something like:

但我期待这样的事情:

3.55872...E-2

or similar.

或类似。

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.

我可以尝试将标签设置为静态,但最终我会有几十或几百个不同值的图,因此需要动态设置。

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

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

采纳答案by tacaswell

There are a number of ways to do this

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

You could just tweak the power limits (doc)

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

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

which set the powers where ScalerFormatterswitches to scientific notation

它设置了ScalerFormatter切换到科学记数法的权力

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

或者,您可以使用 a FuncFormatter,它可以为您提供大量控制(但您可以将脚踢开)。

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 ' + '^{{{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.

FuncFormatter(doc)接受一个 2 参数函数,该函数返回一个字符串并使用该函数来格式化标签。(请注意,这也会改变值在交互式图形角落中的显示方式)。第二个参数是“位置”,这是格式化程序制作标签时传递的参数。你可以放心地忽略它,但你必须接受它(否则你会因错误数量的参数而出错)。这是所有格式化程序统一 API 并使用格式化程序以交互方式显示鼠标位置的结果。

回答by Adobe

You should also specify axis and threshold limits:

您还应该指定轴和阈值限制:

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

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

当数字超出范围时,这将在y轴上使用 sci 格式[0.01, 99]