python + matplotlib:使用locale格式化y轴

otm*_*ger 4 python axis locale matplotlib

我想在python 2.7中使用matplotlib格式化我的y轴.这是我试过的:

ax.yaxis.get_major_formatter().set_useLocale()
Run Code Online (Sandbox Code Playgroud)

使用.千位分隔符格式化我的y轴.而不是拥有10000,我想拥有10.000,等等...但我找不到任何关于这项工作的例子......

我找不到的文档,在此页这里也没有例子或更多的文档:http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.ScalarFormatter.set_useLocale

或者关于如何格式化轴的任何其他想法?

谢谢

cos*_*sis 6

我相信你正在寻找比可能set_useLocale()提供更多的控制.因此,借鉴这里给出的例子,我使用FuncFormatter了一个简单的函数.该comma_format函数使用逗号作为千位分隔符插入y轴标签,然后用句点替换逗号.以这种方式,可以相当容易地格式化y轴标签.

from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

def comma_format(x, p):
    return format(x, "6,.0f").replace(",", ".")

ax = subplot(111)
xx = np.arange(0,20,1)
yy = np.arange(1000,10000,450)
ax.get_yaxis().set_major_formatter(ticker.FuncFormatter(comma_format))
plt.scatter(xx,yy)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述