我正在尝试将我的轴的格式更改为在Python 2.7下运行的Matplotlib中以逗号分隔,但我无法这样做。
我怀疑我需要使用FuncFormatter,但我有点不知所措。
有人可以帮忙吗?
答案 0 :(得分:13)
我知道问题已经过时了,但是由于我目前正在寻找类似的解决方案,所以如果其他人需要,我决定留下评论以供将来参考。
对于替代解决方案,请使用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的解决方案相同的情节,但你也可以确定它在角落情况下的行为也是正确的。
答案 1 :(得分:12)
是的,您可以使用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()
这导致以下情节:
答案 2 :(得分:5)
我认为问题实际上是指在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 need axis
plt.show()
答案 3 :(得分:0)
我想扩展Thorsten Kranz的答案,似乎matplotlib(2.02)可能有一个bug,因为它不会使用该语言环境的千位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
答案 4 :(得分:0)
我想在这里发布另一种解决方案,类似于Thorsten Kranz提出的解决方案。
在代码中使用以下第一行:
.costum-w-80 {
width: 80%;
}
这样,您的代码将被结算为巴西标准文本格式。我相信它可以帮助您。