pgfplots - 图和条的大小与所需值不匹配

pgfplots - 图和条的大小与所需值不匹配

我创建了一个情节普格夫并尝试按照我想要的方式设计它。为此,我选择在块外添加标签和文本axis,因为我想创建几个节点来解释情节。

axis块已经具有选项widthheight/bar/width以及cm值。但是指定的值使用不正确。到目前为止,我只能使整个图的宽度达到所需的宽度。我添加了一些线条作为指导以使其清晰。

在此处输入图片描述

脚本如下:

\documentclass[tikz, 12pt, a4paper]{article} 
\usepackage{tikz}
\usepackage{pgfplots}


\begin{document}

\begin{tikzpicture}

\begin{axis}[
    ybar stacked,
    width = 5cm,
    height = 8cm,
    y axis line style = { opacity = 0 },
    axis x line = none,
    ytick=\empty,
    ymin = 0,
    scale only axis=true,
    bar width=2cm,
    symbolic x coords = {2016,2017},
    every axis plot/.append style={
          fill
        }]

    \addplot[red] coordinates {(2017,47.4) (2016,43.9)};
    \addplot[blue] coordinates {(2017,6.8) (2016,6.3)};
    \addplot[green] coordinates {(2017,18.5) (2016,17.6)};
    \addplot[orange] coordinates {(2017,5.4) (2016,5.2)};
\end{axis}

\draw (0,0) -- (5cm,0) -- (5cm,8cm); % The desired space of entire plot
\draw (2cm,0) -- (2cm,8cm);          %   and the right edge of first bar
\draw (3cm,0) -- (3cm,8cm);          %   and the left edge of second bar

\begin{scope}[xshift=-2cm,yshift=-0.8cm]
    \draw[line width=3mm,red] (0,0) -- ++(0:3mm) node[right, black, align=left] {\footnotesize Lorem Ipsum};
    \draw[line width=3mm,blue] (4,0) -- ++(0:3mm) node[right, black, align=left] {\footnotesize Consetetur Sadipscing};
    \draw[line width=3mm,green] (0,-0.6) -- ++(0:3mm) node[right, black, align=left] {\footnotesize Magna Aliquyam};
    \draw[line width=3mm,orange] (4,-0.6) -- ++(0:3mm) node[right, black, align=left] {\footnotesize Diam Nonumy Eirmod Tempor};
\end{scope}

\end{tikzpicture}

\end{document}

我怎样才能让条形达到我想要的精确宽度?

答案1

这不是一个太严肃的答案,而只是bar width=pi*1cm似乎给了你想要的东西的观察。;-)(可以通过设置来去除垂直多余的部分ymax,所以这更为严重。我想你知道人们可以用不同的方式创造传奇。)

\documentclass[12pt, a4paper]{article} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
    ybar stacked,
    width = 5cm,
    height = 8cm,
    y axis line style = { opacity = 0 },
    axis x line = none,
    axis y line = none,
    ytick=\empty,
    ymin = 0,
    scale only axis=true,
    bar width={pi*1cm},
    ymax=78,
    symbolic x coords = {2016,2017},
    every axis plot/.append style={
          fill
        }]

    \addplot[red] coordinates {(2017,47.4) (2016,43.9)};
    \addplot[blue] coordinates {(2017,6.8) (2016,6.3)};
    \addplot[green] coordinates {(2017,18.5) (2016,17.6)};
    \addplot[orange] coordinates {(2017,5.4) (2016,5.2)};
\end{axis}

\draw (0,0) -- (5cm,0) -- (5cm,8cm); % The desired space of entire plot
\draw (2cm,0) -- (2cm,8cm);          %   and the right edge of first bar
\draw (3cm,0) -- (3cm,8cm);          %   and the left edge of second bar

\begin{scope}[xshift=-2cm,yshift=-0.8cm]
    \draw[line width=3mm,red] (0,0) -- ++(0:3mm) node[right, black, align=left] {\footnotesize Lorem Ipsum};
    \draw[line width=3mm,blue] (4,0) -- ++(0:3mm) node[right, black, align=left] {\footnotesize Consetetur Sadipscing};
    \draw[line width=3mm,green] (0,-0.6) -- ++(0:3mm) node[right, black, align=left] {\footnotesize Magna Aliquyam};
    \draw[line width=3mm,orange] (4,-0.6) -- ++(0:3mm) node[right, black, align=left] {\footnotesize Diam Nonumy Eirmod Tempor};
\end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容