将 tikzpicture 放入图片时出现问题

将 tikzpicture 放入图片时出现问题

我在将此图放入figure声明时遇到问题。当我尝试将此代码放入时figure,我收到此错误:

! Package PGF Math Error: Unknown operator `Y' or `Y0' (in '0Y0.0e0]').

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

我认为该错误是由此行引起的nodes near coords style={at ={(\pgfplotspointmeta,\pgfplotspointy)},anchor=west},但我不知道如何解决这个问题?

代码:

\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{float}
\usepackage{subfigure}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{color}
\usepackage{xcolor}
\usepackage{setspace}
\usepackage{pgfplots}
\usepackage{needspace}
\usepackage{wrapfig}
\usepackage{pgf-pie}
\usepackage{tikz}
\usetikzlibrary{shadows}
\usetikzlibrary{arrows.meta,
                chains,
                calc,
                positioning,
                shapes.geometric
                }

\definecolor{11}{HTML}{685d5c}
\pgfplotsset{compat=1.13}

\begin{document}

\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    ytick={0,1,2,3,4},
    axis y line*=none,
    axis x line*=bottom,
        height=30cm,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    xtick={0,10,...,70},
    width=100mm,
    bar width=3mm,
        ylabel=wiek,
        xlabel=\% respondentów,
    yticklabels={
            {>~55},
            {40~--~55},
            {30~--~39},
        {20~--~29},
            {<~20}
        },
        xmin=-5,
        xmax=70,
        ymax=4,
        xmajorgrids=true,
        grid style=dashed,
    y=6mm,
    enlarge y limits={abs=0.75},
    nodes near coords,
    nodes near coords style={at ={(\pgfplotspointmeta,\pgfplotspointy)},anchor=west},
    visualization depends on=y \as \pgfplotspointy,
    every axis plot/.append style={fill}
]
\addplot[11] coordinates
{(0,0) (0,1) (0,2) (0,3) (9,4) (0,5)};
\addplot[11] coordinates
{(0,0) (0,1) (0,2) (61,3) (0,4) (0,5)};
\addplot[11] coordinates
{(0,0) (0,1) (22,2) (0,3) (0,4) (0,5)};
\addplot[11] coordinates
{(0,0) (7,1) (0,2) (0,3) (0,4) (0,5)};
\addplot[11] coordinates
{(1,0) (0,1) (0,2) (0,3) (0,4) (0,5)};
\end{axis}  
\end{tikzpicture}
\end{figure}

\end{document}

生成的图不含figure

阴谋

答案1

虽然我可以成功运行您的代码,但一个潜在的错误来源可能是在文本模式下使用<和字符。您可以通过在数学模式下使用它们来避免此类问题,即和。>$<$$>$

这是删除不必要的包并在数学模式下使用单个 guillemet 后的代码:

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{float}
\usepackage{graphicx}
\usepackage{pgfplots}
\definecolor{11}{HTML}{685d5c}
\pgfplotsset{compat=1.13}

\begin{document}

\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    ytick={0,1,2,3,4},
    axis y line*=none,
    axis x line*=bottom,
        height=30cm,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    xtick={0,10,...,70},
    width=100mm,
    bar width=3mm,
        ylabel=wiek,
        xlabel=\% respondentów,
    yticklabels={
            {$>$~55},
            {40~--~55},
            {30~--~39},
        {20~--~29},
            {$<$~20}
        },
        xmin=-5,
        xmax=70,
        ymax=4,
        xmajorgrids=true,
        grid style=dashed,
    y=6mm,
    enlarge y limits={abs=0.75},
    nodes near coords,
    nodes near coords style={at ={(\pgfplotspointmeta,\pgfplotspointy)},anchor=west},
    visualization depends on=y \as \pgfplotspointy,
    every axis plot/.append style={fill}
]
\addplot[11] coordinates
{(0,0) (0,1) (0,2) (0,3) (9,4) (0,5)};
\addplot[11] coordinates
{(0,0) (0,1) (0,2) (61,3) (0,4) (0,5)};
\addplot[11] coordinates
{(0,0) (0,1) (22,2) (0,3) (0,4) (0,5)};
\addplot[11] coordinates
{(0,0) (7,1) (0,2) (0,3) (0,4) (0,5)};
\addplot[11] coordinates
{(1,0) (0,1) (0,2) (0,3) (0,4) (0,5)};
\end{axis}  
\end{tikzpicture}
\caption{My figure}
\end{figure}

\end{document}

在此处输入图片描述

相关内容