Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置“时出现Python图形问题”;matplotlib.rcParams[';text.usetex';]=True";在标签中使用LaTeX并使用德语区域设置使用逗号_Python_Matplotlib_Latex_Locale - Fatal编程技术网

设置“时出现Python图形问题”;matplotlib.rcParams[';text.usetex';]=True";在标签中使用LaTeX并使用德语区域设置使用逗号

设置“时出现Python图形问题”;matplotlib.rcParams[';text.usetex';]=True";在标签中使用LaTeX并使用德语区域设置使用逗号,python,matplotlib,latex,locale,Python,Matplotlib,Latex,Locale,我想创建一个图形,其中x和y记号标签显示的数值格式为德语,即使用逗号作为十进制分隔符。我还想在x轴或y轴标签或绘图图例中添加元素。下面的代码显示第一个图形是根据英语版本的需要创建的 # -*- coding: utf-8 -*- import numpy as np import matplotlib import matplotlib.pyplot as plt import locale # Set to German locale to get comma decimal separ

我想创建一个图形,其中x和y记号标签显示的数值格式为德语,即使用逗号作为十进制分隔符。我还想在x轴或y轴标签或绘图图例中添加元素。下面的代码显示第一个图形是根据英语版本的需要创建的

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

import locale

#  Set to German locale to get comma decimal separater
locale.setlocale(locale.LC_NUMERIC, "deu_deu")

# Use LaTeX elements
matplotlib.rcParams['text.usetex'] = True

t = np.linspace(0.0, 1.0, 100)
s = t*np.cos(4 * np.pi * t) + 2

fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(t, s)

ax.set_xlabel(r'Time $t$ with $t \le 1$')
ax.set_ylabel('Velocity $v(t)$')

plt.show()

fig.savefig("Mein_Test1.pdf")


fig2, ax2 = plt.subplots(figsize=(6, 4))
ax2.plot(t, s)

ax2.set_xlabel(r'Time $t$ with $t \le 1$')
ax2.set_ylabel('Velocity $v(t)$')

plt.ticklabel_format(useLocale=True)

plt.show()

fig2.savefig("Mein_Test2.pdf")


如果我不使用“matplotlib.rcParams['text.usetex']=True”,那么我就不能在标签中包含LaTeX元素,即没有“\le”符号,但即使是德语逗号,间距也是正确的。因此,德语区域设置和“text.usetex”之间似乎存在一些冲突。有什么办法可以把两者都做好吗?谢谢

通过查看问题的答案,并使用lambda函数,我最终找到了实现所需结果的方法。代码如下:

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

import locale

# Set to German locale to get comma decimal separater
locale.setlocale(locale.LC_NUMERIC, "deu_deu")

# Use LaTeX elements
mpl.rcParams['text.usetex'] = True

t = np.linspace(0.0, 1.0, 100)
s = t*np.cos(4 * np.pi * t) + 2

fig2, ax2 = plt.subplots(figsize=(6, 4))

ax2.ticklabel_format(useLocale=True)

ax2.plot(t, s)

ax2.set_xlabel(r'Time $t$ with $t \le 1$')
ax2.set_ylabel('Velocity $v(t)$')

plt.ticklabel_format(useLocale=True)


ax2.get_yaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, loc: locale.format_string('%1.3f', x, 1)))
ax2.get_xaxis().set_major_formatter(mpl.ticker.FuncFormatter(lambda x, loc: locale.format_string('%0.2f', x, 2)))

plt.show()

fig2.savefig("Mein_Test2.png")
现在我明白了