3

基本上,当使用 matplotlib 生成绘图时,y 轴上的比例会达到数百万。如何打开数字分组(即让 1000000 显示为 1,000,000)或打开小数分隔符?

4

1 回答 1

3

我认为没有内置功能可以做到这一点。(这就是我阅读您的 Q 后的想法;我刚刚检查过,但在文档中找不到)。

无论如何,很容易自己动手。

(下面是一个完整的例子——即,它将生成一个 mpl 图,其中一个轴具有 commified 刻度标签——尽管创建自定义刻度标签只需要五行代码——函数的三个(包括 import 语句)用于创建自定义标签,两条线用于创建新标签并将它们放置在指定的轴上。)

# first code a function to generate the axis labels you want 
# ie, turn numbers greater than 1000 into commified strings (12549 => 12,549)

import locale
locale.setlocale(locale.LC_ALL, 'en_US')
fnx = lambda x : locale.format("%d", x, grouping=True)

from matplotlib import pyplot as PLT
import numpy as NP

data = NP.random.randint(15000, 85000, 50).reshape(25, 2)
x, y = data[:,0], data[:,1]

fig = PLT.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y, "ro")
default_xtick = range(20000, 100000, 10000)

# these two lines are the crux:
# create the custom tick labels
new_xtick = map(fnx, default_xtick)
# set those labels on the axis
ax1.set_xticklabels(new_xtick)

PLT.show()
于 2010-04-02T13:18:32.280 回答