如何在python中将轴上的点改为逗号

2024-04-25 05:12:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在代码中添加了

import locale
locale.setlocale(locale.LC_NUMERIC, "de_DE")

plt.rcParams['axes.formatter.use_locale'] = True
import matplotlib.pyplot as plt
import numpy as np

它不起作用locale.Error: unsupported locale setting

我测试了我是否有de_DE列在locale -a中,而我没有:

cs_CZ.utf8
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
id_ID.utf8
POSIX

这里是否也有默认的逗号?我认为CZ,但不是,还有点。所以我试着安装deu de

cd /usr/share/locales
./install-language-pack de_DE

然而,结果是

sed: dočasný soubor /etc/sed8Oob4Q nelze otevřít: Operace zamítnuta

这意味着手术是成功的 拒绝。你知道吗

写作而不是locale.setlocale(locale.LC_ALL, 'de_DE')locale.setlocale(locale.LC_ALL, 'de_DE.utf8')也有帮助。你知道吗

我试着跑到终点站

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales

我应该选择像deu de一样的默认语言,尽管我使用另一种语言吗?你知道吗

然后我用export LC_ALL=C什么也没发生。你有没有别的主意用matplotlib逗号代替轴上的点?谢谢


Tags: importmatplotlibasdepltexportutf8all
1条回答
网友
1楼 · 发布于 2024-04-25 05:12:17

如果在加载matplotlib之前调用plt.rcParams,则代码不正确。你知道吗

此代码工作正常:

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['axes.formatter.use_locale'] = True
import locale

print(locale.getlocale()) # default locale, have to be None 
locale.setlocale(locale.LC_NUMERIC, "de") # 'de' for Win or 'de_DE' for linux/mac
print(locale.getlocale()) # test locale after setup
fig, axis = plt.subplots(2, 2)  
plt.show()

enter image description here

相关问题 更多 >