使用 tikzplotlib 保存的带有图例的绘图在乳胶中产生错误

使用 tikzplotlib 保存的带有图例的绘图在乳胶中产生错误

我正在使用 matplotlib 生成一个图,并使用 tikzplotlib 保存它,以便稍后在 latex 中使用。在我使用图例之前,它运行良好。然后 latex 给我以下错误消息:“包 PGF 数学错误:未知函数“None”(在“None”中)。 \end{axis}

问题显然出在生成的 .tex 文件中的以下行:\addlegendentry

虽然图会显示出来,但我想摆脱错误消息。此外,图例中的字体大小看起来不像应该的那样小!

梅威瑟:

Python代码:

import pandas as pd 
import numpy as np
import calendar
from calendar import isleap
from datetime import datetime
import matplotlib 
import matplotlib.pyplot as plt
from my_plot import defSize
import tikzplotlib

t1 = (1,1,1,1)
y1 = (1.1,1.5,2.2,3)
t2 = (2,2,2,2)
y2 = (2.2,2.4,2.5,3)

plt.style.use('seaborn-whitegrid')
plt.figure()
# Plot
plt.title('Calculated Interest Rates for Options expiring on')
plt.xlabel('Time to Maturity in Days')
plt.ylabel('Interest Rates')
plt.xlim(0, 3)
plt.scatter(t1, y1, facecolors='none', edgecolors='k', label = 'OIR with 1 Day of Expiration')
plt.scatter(t2, y2, marker = 's', facecolors='none', edgecolors='r', label = 'OIR with 2 Day of Expiration')
plt.legend(loc="upper right", fontsize=7)
plt.savefig(r'yourDIERECTORY.png')
tikzplotlib.save(r'yourDIRECTORY.tex', axis_width='383.69687',  axis_height='250')

乳胶代码:

\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{tikz}
\usepgfplotslibrary{groupplots,dateplot}
\usetikzlibrary{patterns,shapes.arrows}
\pgfplotsset{compat=newest}
%opening
\title{Problem}
\begin{document}
\maketitle
Here is the problem
\begin{figure}
   \centering
   \label{plot1}
   \input{plot1.tex}
   \caption{\textbf{xxx}. \protect\\ The figure xxx}
\end{figure}
\end{document}

来自 plot1.tex 的代码

% This file was created by tikzplotlib v0.9.2.
\begin{tikzpicture}

\begin{axis}[
axis line style={white!80!black},
height=250,
legend cell align={left},
legend style={fill opacity=None, draw opacity=1, text opacity=1, draw=none},
tick align=outside,
tick pos=both,
title={Calculated Interest Rates for Options expiring on},
width=383.69687,
x grid style={white!80!black},
xlabel={Time to Maturity in Days},
xmajorgrids,
xmin=0, xmax=3,
xtick style={color=white!15!black},
y grid style={white!80!black},
ylabel={Interest Rates},
ymajorgrids,
ymin=0.5, ymax=3.5,
ytick style={color=white!15!black}
]
\addplot [only marks, mark=o, draw=black, colormap/blackwhite]
table{%
x                      y
1 1.1
1 1.5
1 2.2
1 3
};
\addlegendentry{OIR with 1 Day of Expiration}
\addplot [only marks, mark=square, draw=red, colormap/blackwhite]
table{%
x                      y
2 2.2
2 2.4
2 2.5
2 3
};
\addlegendentry{OIR with 2 Day of Expiration}
\end{axis}

\end{tikzpicture}

答案1

错误来自

legend style={fill opacity=None, draw opacity=1, text opacity=1, draw=none},

特别是fill opacity=None。如果你删除该fill opacity设置,或者将其更改None为从 0 到 1 的某个数字,它应该可以工作。

当我运行你的 Python 代码时,我发现了fill opacity=0.8其他一些差异:

8c8
< legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=none},
---
> legend style={fill opacity=None, draw opacity=1, text opacity=1, draw=none},
10c10
< tick pos=left,
---
> tick pos=both,
21c21
< ymin=0.99562181122449, ymax=3.11337053571429,
---
> ymin=0.5, ymax=3.5,

相关内容