The*_*Guy 4 python matplotlib pdflatex
我遇到的问题是使用matplotlib.pyplot创建的pgf输出连字符而不是减号,而Latex无法解释.
我试图使用此处找到的解决方案,但它会在tick标签中将数字从整数更改为浮点数(即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()
Run Code Online (Sandbox Code Playgroud)
mylatex.tex
\documentclass{article}
\usepackage{pgf}
\begin{document}
\begin{figure}[H]
\centering
\input{myplot.pgf}
\end{figure}
\end{document}
Run Code Online (Sandbox Code Playgroud)
如果您在没有formatter参数的情况下进行绘图,则会将标准格式设置为int,但latex将不会将连字符识别为减号.如果使用formatter参数,则所有int都将成为浮点数.
我正在寻找一个解决方案,其中连字符被更改为减号,但无论参数(int或float或其他),刻度将表现为pyplot的默认行为(连字符是减号).
Jea*_*ien 11
默认情况下,Matplotlib的最新版本使用排版"正确"(这是有争议的)unicode减号(U + 2212)来表示负数,而不是ASCII连字符.在我的系统上,在Latex中解释ASCII连字符没有任何问题,但默认情况下不是unicode减号.
使用FuncFormatter与表达return '%i' % x,你提出的在这里减号转换成一个连字符,是乳胶的兼容性有效的解决方案.除了这个解决方案之外,下面还有两个可供选择的解决方案,可以通过更"全系统"的方法解决这个问题.
matplotlib:使用连字符而不是减号
可以使用ASCII连字符(默认情况下由Latex正确解释)代替unicode减号来表示matplotlib中的负数.根据文档(http://matplotlib.org/1.3.0/examples/api/unicode_minus.html),可以通过执行以下操作来执行此操作:
matplotlib.rcParams['axes.unicode_minus'] = False
Run Code Online (Sandbox Code Playgroud)
Latex:使用unicode减号
如果您更喜欢使用减号而不是连字符,可以在Latex文档的前言中添加:
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2212}{$-$}
Run Code Online (Sandbox Code Playgroud)
然后乳胶应识别该字符U+2212并用减号正确表达负数.以下是我使用Latex进行炒作(顶部)和减号(底部)的输出:
| 归档时间: |
|
| 查看次数: |
3481 次 |
| 最近记录: |