我试图将在 Python 2.7 下运行的 Matplotlib 中的轴格式更改为逗号分隔,但我无法这样做。
我怀疑我需要使用 FuncFormatter 但我有点不知所措。
有人可以帮忙吗?
是的,您可以使用
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()
这会产生以下情节:
作为替代解决方案,请使用
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 的解决方案中相同的图,但您可以确定它在极端情况下也能正确运行。
我认为这个问题实际上是指将 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的答案,似乎matplotlib(2.02)可能有一个错误,因为它不使用语言环境的千位分隔符字段来进行千位分隔。即使使用 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': '.'}
编辑: 查看标量格式化程序中的代码,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
我想在这里发布另一种解决方案,类似于 Thorsten Kranz 提出的解决方案。
在代码中使用以下第一行:
import locale
locale.setlocale(locale.LC_ALL, "Portuguese_Brazil.1252")
import matplotlib as mpl
mpl.rcParams['axes.formatter.use_locale'] = True
这样,您的代码将符合巴西标准文本格式。我相信它可以帮助你。