Matplotlib 对数刻度刻度标签数字格式 [英] Matplotlib log scale tick label number formatting

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

问题描述

使用 matplotlib 为轴指定对数刻度时,标记该轴的默认方法是使用 10 次幂的数字,例如.10^6.有没有一种简单的方法可以将所有这些标签更改为它们的完整数字表示?例如.1、10、100 等

With matplotlib when a log scale is specified for an axis, the default method of labeling that axis is with numbers that are 10 to a power eg. 10^6. Is there an easy way to change all of these labels to be their full numerical representation? eg. 1, 10, 100, etc.

请注意,我不知道权力的范围是多少,并希望支持任意范围(包括负数).

Note that I do not know what the range of powers will be and want to support an arbitrary range (negatives included).

推荐答案

当然,只需更改格式化程序即可.

Sure, just change the formatter.

例如,如果我们有这个图:

For example, if we have this plot:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.axis([1, 10000, 1, 100000])
ax.loglog()

plt.show()

您可以手动设置刻度标签,但是当您缩放/平移/等时,刻度位置和标签将被固定.因此,最好更改格式化程序.默认情况下,对数刻度使用 LogFormatter,它将以科学记数法格式化值.要将格式化程序更改为线性轴的默认值 (ScalarFormatter),请使用例如

You could set the tick labels manually, but then the tick locations and labels would be fixed when you zoom/pan/etc. Therefore, it's best to change the formatter. By default, a logarithmic scale uses a LogFormatter, which will format the values in scientific notation. To change the formatter to the default for linear axes (ScalarFormatter) use e.g.

from matplotlib.ticker import ScalarFormatter
for axis in [ax.xaxis, ax.yaxis]:
    axis.set_major_formatter(ScalarFormatter())

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

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