在 Matplotlib 中使用连字符或减号与 Latex 的兼容性
Use of Hyphen or Minus Sign in Matplotlib versus Compatibility with Latex
我遇到问题,使用 matplotlib.pyplot 创建的 pgf 输出连字符而不是减号,Latex 无法解释。
我尝试使用 here 找到的解决方案,但它将刻度标签中的数字从整数更改为浮点数(即 2000 变为 2000.0)。我正在寻找一种修复标志但在 pyplot 中保留默认格式的解决方案。
有什么想法吗?示例如下。
myplot.py
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
def math_formatter(x, pos):
return "$%s$" % x
plt.figure()
plt.plot([1,2,3,4,5,6,7],[-1,-2,3,4,5,6,7])
axis = plt.gca()
axis.xaxis.set_major_formatter(FuncFormatter(math_formatter))
axis.yaxis.set_major_formatter(FuncFormatter(math_formatter))
plt.show()
mylatex.tex
\documentclass{article}
\usepackage{pgf}
\begin{document}
\begin{figure}[H]
\centering
\input{myplot.pgf}
\end{figure}
\end{document}
如果你在没有格式化参数的情况下绘图,你会得到标准格式作为 int,但 Latex 不会将连字符识别为减号。如果你使用 formatter 参数,所有的 int 都会变成 floats。
我正在寻找一种将连字符更改为减号的解决方案,但无论参数(int 或 float 或其他)如何,刻度线的行为都类似于 pyplot 的默认行为(除了连字符是减号)。
尝试这样的事情:
def math_formatter(x, pos):
return "${}$".format(x).replace.("-", u"\u2212")
解决方案是简单地转换为整数。
def math_formatter(x, pos):
return "%i" %x
最新版本的 Matplotlib 默认使用印刷 "correct"(这是有争议的)unicode minus signs (U+2212) to express negative numbers, and not the ASCII hyphens。在我的系统上,ASCII 连字符在 Latex 中的解释没有任何问题,但 unicode 减号不是默认的。
使用 FuncFormatter
和表达式 return '%i' % x
正如您所建议的 将减号转换为连字符,并且是 Latex 兼容性的有效解决方案。除了此解决方案之外,下面还有两个额外的替代解决方案,可用于通过更 "system-wide" 的方法解决此问题。
matplotlib : 使用连字符代替减号
在 matplotlib 中可以使用 ASCII 连字符(默认情况下 Latex 正确解释)代替 unicode 减号来表示负数。根据文档 (http://matplotlib.org/1.3.0/examples/api/unicode_minus.html),可以通过以下方式做到这一点:
matplotlib.rcParams['axes.unicode_minus'] = False
Latex : 使用 unicode 减号
如果您更喜欢使用减号而不是连字符,您可以在 Latex 文档的序言中添加:
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2212}{$-$}
Latex 应能识别字符 U+2212
并用减号正确表示负数。下面是我使用带有连字符(顶部)和减号(底部)的 Latex 得到的输出:
我遇到问题,使用 matplotlib.pyplot 创建的 pgf 输出连字符而不是减号,Latex 无法解释。
我尝试使用 here 找到的解决方案,但它将刻度标签中的数字从整数更改为浮点数(即 2000 变为 2000.0)。我正在寻找一种修复标志但在 pyplot 中保留默认格式的解决方案。
有什么想法吗?示例如下。
myplot.py
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
def math_formatter(x, pos):
return "$%s$" % x
plt.figure()
plt.plot([1,2,3,4,5,6,7],[-1,-2,3,4,5,6,7])
axis = plt.gca()
axis.xaxis.set_major_formatter(FuncFormatter(math_formatter))
axis.yaxis.set_major_formatter(FuncFormatter(math_formatter))
plt.show()
mylatex.tex
\documentclass{article}
\usepackage{pgf}
\begin{document}
\begin{figure}[H]
\centering
\input{myplot.pgf}
\end{figure}
\end{document}
如果你在没有格式化参数的情况下绘图,你会得到标准格式作为 int,但 Latex 不会将连字符识别为减号。如果你使用 formatter 参数,所有的 int 都会变成 floats。
我正在寻找一种将连字符更改为减号的解决方案,但无论参数(int 或 float 或其他)如何,刻度线的行为都类似于 pyplot 的默认行为(除了连字符是减号)。
尝试这样的事情:
def math_formatter(x, pos):
return "${}$".format(x).replace.("-", u"\u2212")
解决方案是简单地转换为整数。
def math_formatter(x, pos):
return "%i" %x
最新版本的 Matplotlib 默认使用印刷 "correct"(这是有争议的)unicode minus signs (U+2212) to express negative numbers, and not the ASCII hyphens。在我的系统上,ASCII 连字符在 Latex 中的解释没有任何问题,但 unicode 减号不是默认的。
使用 FuncFormatter
和表达式 return '%i' % x
正如您所建议的
matplotlib : 使用连字符代替减号
在 matplotlib 中可以使用 ASCII 连字符(默认情况下 Latex 正确解释)代替 unicode 减号来表示负数。根据文档 (http://matplotlib.org/1.3.0/examples/api/unicode_minus.html),可以通过以下方式做到这一点:
matplotlib.rcParams['axes.unicode_minus'] = False
Latex : 使用 unicode 减号
如果您更喜欢使用减号而不是连字符,您可以在 Latex 文档的序言中添加:
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2212}{$-$}
Latex 应能识别字符 U+2212
并用减号正确表示负数。下面是我使用带有连字符(顶部)和减号(底部)的 Latex 得到的输出: