Skip to content

tikzplotlib should have a legend for seaborn #613

Description

@JasonGross
import matplotlib.pyplot as plt
import seaborn as sns
import tikzplotlib
import numpy as np
np.random.seed(42)
data = np.random.rand(4, 2)
fig, ax = plt.subplots()
sns.scatterplot({str(i): d for i, d in enumerate(data)}, ax=ax)
print(tikzplotlib.get_tikz_code(fig))
plt.close(fig)
fig, ax = plt.subplots()
for i, d in enumerate(data):
    x = range(len(d))
    plt.scatter(x, d, label=str(i))
plt.legend()
# plt.show()
print(tikzplotlib.get_tikz_code(fig))
plt.close(fig)

gives

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

\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{lightgray204}{RGB}{204,204,204}

\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=lightgray204
},
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xmin=-0.05, xmax=1.05,
xtick style={color=black},
y grid style={darkgray176},
ymin=0.0134520774561136, ymax=0.995345841122002,
ytick style={color=black}
]
\addplot [
  draw=white,
  forget plot,
  mark=*,
  only marks,
  scatter,
  scatter/@post marker code/.code={%
  \endscope
},
  scatter/@pre marker code/.code={%
  \expanded{%
  \noexpand\definecolor{thispointfillcolor}{RGB}{\fillcolor}%
  }%
  \scope[fill=thispointfillcolor]%
},
  visualization depends on={value \thisrow{fill} \as \fillcolor}
]
table{%
x  y  fill
0 0.374540118847362 31,119,180
1 0.950714306409916 31,119,180
0 0.731993941811405 255,127,14
1 0.598658484197037 255,127,14
0 0.156018640442437 44,160,44
1 0.155994520336203 44,160,44
0 0.0580836121681995 214,39,40
1 0.866176145774935 214,39,40
};
\end{axis}

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

\definecolor{crimson2143940}{RGB}{214,39,40}
\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{darkorange25512714}{RGB}{255,127,14}
\definecolor{forestgreen4416044}{RGB}{44,160,44}
\definecolor{lightgray204}{RGB}{204,204,204}
\definecolor{steelblue31119180}{RGB}{31,119,180}

\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=lightgray204
},
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xmin=-0.05, xmax=1.05,
xtick style={color=black},
y grid style={darkgray176},
ymin=0.0134520774561136, ymax=0.995345841122002,
ytick style={color=black}
]
\addplot [draw=steelblue31119180, fill=steelblue31119180, mark=*, only marks]
table{%
x  y
0 0.374540118847362
1 0.950714306409916
};
\addlegendentry{0}
\addplot [draw=darkorange25512714, fill=darkorange25512714, mark=*, only marks]
table{%
x  y
0 0.731993941811405
1 0.598658484197037
};
\addlegendentry{1}
\addplot [draw=forestgreen4416044, fill=forestgreen4416044, mark=*, only marks]
table{%
x  y
0 0.156018640442437
1 0.155994520336203
};
\addlegendentry{2}
\addplot [draw=crimson2143940, fill=crimson2143940, mark=*, only marks]
table{%
x  y
0 0.0580836121681995
1 0.866176145774935
};
\addlegendentry{3}
\end{axis}

\end{tikzpicture}

Note that the second one has a legend but the first does not. This is because

if label in d:

fails, because the single line collection is labeled _child0 instead of having a series label

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions