简体   繁体   English

设置“matplotlib.rcParams['text.usetex'] = True”以在标签中使用LaTeX并使用德语语言环境使用逗号时的Python图形问题

[英]Python figure problem when setting "matplotlib.rcParams['text.usetex'] = True" to use LaTeX in labels AND using the German locale to use the comma

I want to create figures in which the x and y tick labels show their numerical values properly formatted for the German language, ie, using the comma as the decimal separator.我想创建数字,其中 x 和 y 刻度标签显示它们为德语正确格式化的数值,即,使用逗号作为小数点分隔符。 I also want to LaTeX elements eg in the x or y axis label or the plot legend.我还想在 x 或 y 轴标签或绘图图例中添加 LaTeX 元素。 The following code shows that the first figure is created as desired for the English language version.以下代码显示第一个图形是根据英语语言版本的需要创建的。

# -*- 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")

The first figure looks as desired, with nice English points as decimal separators and nice spacing in the axis tick labels, and I can use the LaTeX "\\le" symbol in the x axix label:第一个数字看起来像需要的一样,用漂亮的英文点作为小数点分隔符,轴刻度标签中的间距很好,我可以在 x 轴标签中使用 LaTeX "\\le" 符号:

However, the second figure shows an ugly spacing in the numbers of axis tick labels, with too much space behind the decimal separator:然而,第二张图显示了轴刻度标签数量的丑陋间距,小数点分隔符后面有太多空间:

If I do not use "matplotlib.rcParams['text.usetex'] = True", then I cannot have the LaTeX elements in the label, ie, no "\\le" symbol, but then the spacing is correct even for the German commas.如果我使用“matplotlib.rcParams['text.usetex'] = True”,那么我不能在标签中包含 LaTeX 元素,即没有“\\le”符号,但是即使对于德语,间距也是正确的逗号。 So there seems to be some conflict here between the German locale and the "text.usetex".因此,德语语言环境和“text.usetex”之间似乎存在一些冲突。 Any ideas to get both right?有什么想法可以让两者都正确吗? Thanks!谢谢!

I eventually found a way to achieve the desired result by looking at the answer to this question, using also lambda function.通过查看这个问题的答案,我最终找到了一种方法来实现预期的结果,也使用 lambda 函数。 Here is the code:这是代码:

# -*- 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")

Now I get the desired result:现在我得到了想要的结果:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM