2

I am generating a plot with matplotlib and save it with tikzplotlib to use it later in latex. It works fine till the point where I use a legend in the plot. Then latex gives me the following Error Message: "Package PGF Math Error: Unknown function `None' (in 'None'). \end{axis}"

The problem is apparently with the following line in the generated .tex file: \addlegendentry

Though the plot will appear I want to get rid from the error message. Furthermore the font size in the legend doesn't appear as small as it should!

MWE:

Python Code:

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')

Latex Code:

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

Code from 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}
2
  • Hi, welcome. This works fine for me, can you show the code in plot1.tex as well? Jun 8, 2020 at 15:01
  • Added the code from plot1.tex in my post. This throws me an error message. Removing now all the \addlegendentry results in no error message. But I need the legend Jun 8, 2020 at 15:14

1 Answer 1

1

The error comes from

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

in particular fill opacity=None. If you remove the fill opacity setting, or change None to some number from 0 to 1, it should work.

When I ran your Python code I got fill opacity=0.8 and a couple of other differences:

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,
2
  • Though, I still an issue with the font size from my legende. The font size in my legend is the same as my axis title, although I put the font size to 7. Do you know how to solve that? Jun 8, 2020 at 18:34
  • @C0deP0ntage You can add font=\footnotesize (or \small, or \scriptsize, or \tiny) to the legend style. Jun 8, 2020 at 18:36

You must log in to answer this question.

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