matplotlib2tikz 图中缺少插入 {

matplotlib2tikz 图中缺少插入 {

使用matplotlib创建条形图后,我使用matplotlib2tikz创建tikz图形。 代码如下:

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

在日志文件中,我收到错误

Missing character: There is no ; in font nullfont!

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

其次是

./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$}'.

知道这里出了什么问题吗?

答案1

当我构建代码时@esdd 已经给出了正确答案,您必须应用 \addplot 才能使用 \legend 条目。请看这里

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

相关内容