1

I'm getting an error when I generate a (line)plot with either matplotlib or Seaborn and save it with tikzplotlib to use it later in latex. Everything works until I add a legend to the plot.

This completely hinders compiling of the entire document. Could someone help me out?

Here's how I generate the table in 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")

Here's how the code looks in 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}

I also noticed that there was another person who had asked the same thing a couple of years ago, but the solution is not working: using plot with legend saved with tikzplotlib gives Error in latex

1
  • 1
    This is due to the _ you provide, for example, in \addlegendentry{n_blob}. You need to escape this: \addlegendentry{n\_blob}. Sep 24, 2022 at 12:39

1 Answer 1

1

You try to add strings that contain an underscore (_) to the legend, and underscores lead TeX to think that the following is meant to be a subscript which has to be in math mode, which leads to an error.

You probably want to escape these underscores using a backslash, like in \_:

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .