Matplotlib对数刻度刻度标签数字格式 [英] Matplotlib log scale tick label number formatting
问题描述
对于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屋!