0

After using matplotlib to create a bar chart, I use matplotlib2tikz to create a tikz figure. The code is as following:

% This file was created by matplotlib2tikz v0.6.13.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.75,0,0.75}

\begin{axis}[
title={Gender distribution},
xlabel={Age},
ylabel={Number of people},
xmin=-0.22, xmax=2.42,
ymin=0, ymax=10.5,
xtick={0.1,1.1,2.1},
xticklabels={{14-19},{20-27},{28-35}},
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black},
legend style={draw=white!80.0!black},
legend entries={{Male},{Female}},
legend cell align={left}
]
\draw[fill=blue,draw opacity=0] (axis cs:-0.1,0) rectangle (axis cs:0.1,6);
\draw[fill=blue,draw opacity=0] (axis cs:0.9,0) rectangle (axis cs:1.1,3);
\draw[fill=blue,draw opacity=0] (axis cs:1.9,0) rectangle (axis cs:2.1,7);
\draw[fill=color0,draw opacity=0] (axis cs:0.1,0) rectangle (axis cs:0.3,4);
\draw[fill=color0,draw opacity=0] (axis cs:1.1,0) rectangle (axis cs:1.3,10);
\draw[fill=color0,draw opacity=0] (axis cs:2.1,0) rectangle (axis cs:2.3,6);
\end{axis}

\end{tikzpicture}

In the log file, I get the error

Missing character: There is no ; in font nullfont!

./test.tex:28: Missing } inserted.
<inserted text> 
            }
l.28 \end{axis}

followed by

./test.tex:28: Package tikz Error: Giving up on this path. Did you forget a semicolon?.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.28 \end{axis}

This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.

./test.tex:28: Extra }, or forgotten \endgroup.
\endpgfpicture ...globally \endgroup \hss \egroup 
                                                      \pgf@restore@layerlist@fro...
l.28 \end{axis}

I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

Any ideas what's wrong here?

  • I got a problem with \begin{axis}. – Sigur Sep 20 '17 at 13:07
  • There are legend entriesset - without any \addplot commands. Remove the legend entries from the axis settings or add ybar to the axis options and \addlegendimage{ybar legend,fill=blue,draw opacity=0} and \addlegendimage{ybar legend,fill=color0,draw opacity=0} inside the axis environment. – esdd Sep 20 '17 at 13:26
0

While I was building the code @esdd already gave the correct answer, you have to apply \addplot to use \legend entries. Here you go

\documentclass [tikz] {standalone}

\usepackage{pgfplots} 

\begin{document}

\begin{tikzpicture}

\definecolor{color0}{rgb}{0.75,0,0.75}

\begin{axis}[
ybar,
bar width=15pt,
xlabel = Age,
ylabel = Number of people,
xmin=-0.22, xmax=2.42,
ymin=0, ymax=10.5,
xtick={0.1,1.1,2.1},
xticklabels={{14-19},{20-27},{28-35}},
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black},
enlargelimits=false,
legend style={draw=white!80.0!black},
legend cell align={left},
legend entries={Male, Female},
]

\addplot[fill=blue,draw opacity=0] coordinates {(0.1,6) (1.1,3) (2.1,7)};    
\addplot[fill=color0,draw opacity=0] coordinates {(0.1,4) (1.1,10) (2.1,6)};

\end{axis}

\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.