保存带有图例的图 tikzplotlib 在 latex 中出现错误

保存带有图例的图 tikzplotlib 在 latex 中出现错误

当我使用 matplotlib 或 Seaborn 生成(线)图并使用 tikzplotlib 保存以便稍后在 latex 中使用时,出现错误。在我向图中添加图例之前,一切正常。

这完全阻碍了整个文档的编译。有人能帮我吗?

以下是我在 Python 中生成表格的方法:

sns.lineplot(data = all_ar[["AAR_full", "AAR_n_blob", "AAR_p_blob", "AAR_n_flair", "AAR_p_flair", "AAR_n_vader", "AAR_p_vader"]])
plt.ylabel("Abnormal return (%)")
plt.legend(["full", "n_blob", "p_blob", "n_flair", "p_flair", "n_vader", "p_vader"], bbox_to_anchor=(1.02, 0.55), loc='upper left', borderaxespad=0)
tikzplotlib.save(
    r"/Users/.../src/figures/ave_stock.tex")

代码在 Latex 中的样子如下:

% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}

\definecolor{chocolate2267451}{RGB}{226,74,51}
\definecolor{dimgray85}{RGB}{85,85,85}
\definecolor{gainsboro229}{RGB}{229,229,229}
\definecolor{gray119}{RGB}{119,119,119}
\definecolor{lightgray204}{RGB}{204,204,204}
\definecolor{lightpink255181184}{RGB}{255,181,184}
\definecolor{mediumpurple152142213}{RGB}{152,142,213}
\definecolor{sandybrown25119394}{RGB}{251,193,94}
\definecolor{steelblue52138189}{RGB}{52,138,189}
\definecolor{yellowgreen14218666}{RGB}{142,186,66}

\begin{axis}[
axis background/.style={fill=gainsboro229},
axis line style={white},
legend cell align={left},
legend style={
  fill opacity=0.8,
  draw opacity=1,
  text opacity=1,
  at={(1.02,0.55)},
  anchor=north west,
  draw=lightgray204,
  fill=gainsboro229
},
tick align=outside,
x grid style={white},
xmajorgrids,
xmajorticks=false,
xmin=-0.5, xmax=10.5,
xtick style={color=dimgray85},
y grid style={white},
ylabel=\textcolor{dimgray85}{Abnormal return (\%)},
ymajorgrids,
ymajorticks=false,
ymin=-0.287696053022948, ymax=0.161506521759366,
ytick style={color=dimgray85}
]
\addplot [semithick, chocolate2267451]
table {%
0 0.00203852378906001
1 0.00134780834384111
2 0.0208583154192638
3 -0.0245665065514231
4 -0.0112441737141541
5 0.0731085591860976
6 0.0328666959368619
7 -0.0063124981609702
8 0.0143959028392461
9 0.00633946996392669
10 0.00538879313604582
};
\addlegendentry{full}
\addplot [semithick, steelblue52138189, dash pattern=on 4pt off 1.5pt]
table {%
0 0.0939391436091629
1 -0.0440298273903353
2 0.090710895198631
3 -0.0385036717953371
4 -0.00844356105487198
5 -0.109865478375522
6 0.0035702561476586
7 0.0087983232698903
8 0.00799507390360541
9 -0.0296297041102631
10 0.128012759972556
};
\addlegendentry{n_blob}
\addplot [semithick, mediumpurple152142213, dash pattern=on 1pt off 1pt]
table {%
0 0.00335483818550164
1 0.0354668186460481
2 0.0441697867472804
3 0.000774113096407794
4 -0.00867399711754033
5 0.0455372862226543
6 0.0716848946466507
7 0.0133450032803731
8 0.0288810793942865
9 0.0442463305982716
10 -0.0337859117227515
};
\addlegendentry{p_blob}
\addplot [semithick, gray119, dash pattern=on 3pt off 1.25pt on 1.5pt off 1.25pt]
table {%
0 -0.036615203001422
1 0.0101702303338121
2 0.00174947852754255
3 -0.0190701319072389
4 -0.0160841935303124
5 0.0681032265408471
6 0.0550490433324712
7 -0.0129419921814171
8 0.0306135400776931
9 -0.0798413649405097
10 -0.00275854796697788
};
\addlegendentry{n_flair}
\addplot [semithick, sandybrown25119394, dash pattern=on 5pt off 1pt on 1pt off 1pt]
table {%
0 0.0265173198419426
1 -0.00423929247454994
2 0.0329596406967897
3 -0.0280472739991392
4 -0.00817906543588003
5 0.0762783574739588
6 0.0188189648910416
7 -0.00211414406849277
8 0.00412552874126058
9 0.0609164346309482
10 0.010548375866974
};
\addlegendentry{p_flair}
\addplot [semithick, yellowgreen14218666, dash pattern=on 3pt off 1.25pt on 1.25pt off 1.25pt on 1.25pt off 1.25pt]
table {%
0 0.000333470087199028
1 -0.0804807131314031
2 0.141088222905625
3 0.076597664040136
4 -0.049938519089083
5 -0.267277754169207
6 0.0336553194558998
7 -0.120009491785485
8 0.0854173710466596
9 -0.0208168331735932
10 0.0416660646822646
};
\addlegendentry{n_vader}
\addplot [semithick, lightpink255181184, dash pattern=on 4pt off 1pt on 4pt off 1pt on 1pt off 1pt]
table {%
0 -0.0285035896433093
1 0.0165204609379901
2 0.0239289051765231
3 -0.0384167524728991
4 -0.0144415939151033
5 0.0826317496636438
6 0.057843816816002
7 0.0355877545917432
8 -0.00529711911516038
9 0.0642832041610509
10 0.00698811575892561
};
\addlegendentry{p_vader}
\end{axis}

\end{tikzpicture}

我还注意到几年前也有人问过同样的问题,但是解决方案不起作用:使用 tikzplotlib 保存的带有图例的绘图在乳胶中产生错误

答案1

您尝试向图例添加包含下划线 ( _) 的字符串,下划线会导致 TeX 认为以下内容是必须处于数学模式的下标,从而导致错误。

您可能希望使用反斜杠来转义这些下划线,例如\_

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}

\definecolor{chocolate2267451}{RGB}{226,74,51}
\definecolor{dimgray85}{RGB}{85,85,85}
\definecolor{gainsboro229}{RGB}{229,229,229}
\definecolor{gray119}{RGB}{119,119,119}
\definecolor{lightgray204}{RGB}{204,204,204}
\definecolor{lightpink255181184}{RGB}{255,181,184}
\definecolor{mediumpurple152142213}{RGB}{152,142,213}
\definecolor{sandybrown25119394}{RGB}{251,193,94}
\definecolor{steelblue52138189}{RGB}{52,138,189}
\definecolor{yellowgreen14218666}{RGB}{142,186,66}

\begin{axis}[
axis background/.style={fill=gainsboro229},
axis line style={white},
legend cell align={left},
legend style={
  fill opacity=0.8,
  draw opacity=1,
  text opacity=1,
  at={(1.02,0.55)},
  anchor=north west,
  draw=lightgray204,
  fill=gainsboro229
},
tick align=outside,
x grid style={white},
xmajorgrids,
xmajorticks=false,
xmin=-0.5, xmax=10.5,
xtick style={color=dimgray85},
y grid style={white},
ylabel=\textcolor{dimgray85}{Abnormal return (\%)},
ymajorgrids,
ymajorticks=false,
ymin=-0.287696053022948, ymax=0.161506521759366,
ytick style={color=dimgray85}
]
\addplot [semithick, chocolate2267451]
table {%
0 0.00203852378906001
1 0.00134780834384111
2 0.0208583154192638
3 -0.0245665065514231
4 -0.0112441737141541
5 0.0731085591860976
6 0.0328666959368619
7 -0.0063124981609702
8 0.0143959028392461
9 0.00633946996392669
10 0.00538879313604582
};
\addlegendentry{full}
\addplot [semithick, steelblue52138189, dash pattern=on 4pt off 1.5pt]
table {%
0 0.0939391436091629
1 -0.0440298273903353
2 0.090710895198631
3 -0.0385036717953371
4 -0.00844356105487198
5 -0.109865478375522
6 0.0035702561476586
7 0.0087983232698903
8 0.00799507390360541
9 -0.0296297041102631
10 0.128012759972556
};
\addlegendentry{n\_blob}

% etc.

\end{axis}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容