【发布时间】:2011-11-25 16:02:37
【问题描述】:
我正在尝试将轴的格式更改为在 Python 2.7 下运行的 Matplotlib 中以逗号分隔,但我无法这样做。
我怀疑我需要使用 FuncFormatter,但我有点不知所措。
谁能帮忙?
【问题讨论】:
标签: python matplotlib
我正在尝试将轴的格式更改为在 Python 2.7 下运行的 Matplotlib 中以逗号分隔,但我无法这样做。
我怀疑我需要使用 FuncFormatter,但我有点不知所措。
谁能帮忙?
【问题讨论】:
标签: python matplotlib
我想扩展 Thorsten Kranz 的答案,似乎 matplotlib (2.02) 可能有一个错误,因为它不使用语言环境的千位 sep 字段来分隔千位。即使使用 set_locale(True) 也会发生这种情况。
因此,如果您将语言环境设置为英国语言环境,它仍应以逗号分隔千位,但事实并非如此。由于使用了小数点,因此它适用于德语语言环境。
英国('English_United Kingdom.1252')语言环境:
{'currency_symbol': '\xa3',
'decimal_point': '.',
'frac_digits': 2,
'grouping': [3, 0],
'int_curr_symbol': 'GBP',
'int_frac_digits': 2,
'mon_decimal_point': '.',
'mon_grouping': [3, 0],
'mon_thousands_sep': ',',
'n_cs_precedes': 1,
'n_sep_by_space': 0,
'n_sign_posn': 3,
'negative_sign': '-',
'p_cs_precedes': 1,
'p_sep_by_space': 0,
'p_sign_posn': 3,
'positive_sign': '',
'thousands_sep': ','}
德语('German_Germany.1252')语言环境:
{'currency_symbol': '\x80',
'decimal_point': ',',
'frac_digits': 2,
'grouping': [3, 0],
'int_curr_symbol': 'EUR',
'int_frac_digits': 2,
'mon_decimal_point': ',',
'mon_grouping': [3, 0],
'mon_thousands_sep': '.',
'n_cs_precedes': 0,
'n_sep_by_space': 1,
'n_sign_posn': 1,
'negative_sign': '-',
'p_cs_precedes': 0,
'p_sep_by_space': 1,
'p_sign_posn': 1,
'positive_sign': '',
'thousands_sep': '.'}
编辑: 查看 Scalar 格式化程序中的代码,Matplotlib 没有使用分组标志:
def pprint_val(self, x):
"""The last argument should be True"""
xp = (x - self.offset) / (10. ** self.orderOfMagnitude)
if np.absolute(xp) < 1e-8:
xp = 0
if self._useLocale:
return locale.format_string(self.format, (xp,)) # <-- there should be a True as the last argument to this method which sets to grouping to True
else:
return self.format % xp
【讨论】:
我知道这个问题已经过时了,但由于我目前正在寻找类似的解决方案,因此如果其他人需要,我决定留下评论以供将来参考。
对于替代解决方案,请使用 locale 模块并在 matplotlib 中激活区域设置格式。
例如,在欧洲的主要地区,逗号是所需的分隔符。你可以使用
#Locale settings
import locale
locale.setlocale(locale.LC_ALL, "deu_deu")
import matplotlib as mpl
mpl.rcParams['axes.formatter.use_locale'] = True
#Generate sample plot
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,501)
y = np.sin(x)
ax = plt.subplot(111)
ax.plot(x,y)
ax.yaxis.set_major_formatter(y_format) # set formatter to needed axis
plt.show()
生成与 Andrey 的解决方案相同的图,但您可以确保它在极端情况下也能正确运行。
【讨论】:
locale -a 输出以获取可能的值。也不需要设置格式化程序(C&P错误)
是的,您可以使用matplotlib.ticker.FuncFormatter 来执行此操作。
示例如下:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
def func(x, pos): # formatter function takes tick label and tick position
s = str(x)
ind = s.index('.')
return s[:ind] + ',' + s[ind+1:] # change dot to comma
y_format = tkr.FuncFormatter(func) # make formatter
x = np.linspace(0,10,501)
y = np.sin(x)
ax = plt.subplot(111)
ax.plot(x,y)
ax.yaxis.set_major_formatter(y_format) # set formatter to needed axis
plt.show()
这将导致以下情节:
【讨论】:
func,可以简单地使用字符串格式函数内置的逗号分隔功能,像这样:FuncFormatter('{:,.0f}'.format)
0 标记。
ax.get_yaxis().set_major_formatter( ticker.FuncFormatter(lambda x, pos: str(x).replace('.',',')) )而不是定义一个sepcial函数func(x, pos)
0.05 替换为 0,05,但将 0.10 替换为 0,1。如何获取0,10?
lambda 函数修改为lambda x, pos: '{:.2f}'.format(x).replace('.', ',')
我认为这个问题实际上是指将 y 轴上的 300000 表示为 300,000。
借用安德烈的回答,稍作调整,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
def func(x, pos): # formatter function takes tick label and tick position
s = '{:0,d}'.format(int(x))
return s
y_format = tkr.FuncFormatter(func) # make formatter
x = np.linspace(0,10,501)
y = np.sin(x)
ax = plt.subplot(111)
ax.plot(x,y)
ax.yaxis.set_major_formatter(y_format) # set formatter to needed axis
plt.show()
【讨论】:
我想在这里发布另一个解决方案,类似于 Thorsten Kranz 提出的解决方案。
在您的代码中使用以下第一行:
import locale
locale.setlocale(locale.LC_ALL, "Portuguese_Brazil.1252")
import matplotlib as mpl
mpl.rcParams['axes.formatter.use_locale'] = True
这样,您的代码将采用巴西标准文本格式。我相信它可以帮助你。
【讨论】: