tikzplotlib icon indicating copy to clipboard operation
tikzplotlib copied to clipboard

Legend title is not converted to tikz

Open Mpedrosab opened this issue 5 years ago • 2 comments
trafficstars

When creating a legend with a title, the title does not transform to tikz code.

Python code:

import matplotlib.pyplot as plt
from tikzplotlib import save as tikz_save

x=[-1,1]
y=[-1,1]
plt.figure()
plt.plot(x,y,label='my legend')

#plt.xlim([1e-3,5])
#plt.ylim([1e-3,1e2])
plt.legend(title='my title')
#plt.savefig("test.png")
tikz_save("test.tikz",encoding ='utf-8') 

Tikz output:

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

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
legend cell align={left},
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, at={(0.03,0.97)}, anchor=north west, draw=white!80!black},
tick align=outside,
tick pos=both,
x grid style={white!69.0196078431373!black},
xmin=-1.1, xmax=1.1,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-1.1, ymax=1.1,
ytick style={color=black}
]
\addplot [semithick, color0]
table {%
-1 -1
1 1
};
\addlegendentry{my legend}
\end{axis}

\end{tikzpicture}

Expected output:

image

Obtained output:

image

Mpedrosab avatar Apr 12 '20 11:04 Mpedrosab

That's because pgfplots doesn't support legend titles yet, see https://github.com/pgf-tikz/pgfplots/issues/307.

nschloe avatar Feb 08 '21 11:02 nschloe

its the same even now. Hopefully it will be sorted out in future. Between to resolve this, I need to make changes in .tex file itself. I need to add following lines of codes to get the legend title

legend style={
    cells={anchor=east},
    legend pos=north east,
    draw=lightgray204, % Remove the legend border
    fill=none, % Remove the legend background
    /tikz/every even column/.append style={column sep=1pt}, % Adjust the column separatio
    /tikz/every odd column/.append style={column sep=1pt}, % Adjust the column separationn
    row sep=1pt, % Adjust the row separation
    nodes={inner sep=1pt}, % Adjust the padding of legend entries
}
]
% Add legend title "Wave"
\addlegendimage{white, fill=white} % Dummy entry for legend title
\addlegendentry{Wave}

VijayN10 avatar Jul 25 '23 12:07 VijayN10