pgfplots ybar 问题

pgfplots ybar 问题

我在使用 ybar 进行 pgfplots 时遇到了问题。当我尝试:

\begin{tikzpicture}
\begin{axis}[height=4.5cm, width=8cm, ybar, axis x line=bottom, axis y line=left, grid=none, ytick={0,10,20}, xmin=0, xmax=7, xtick={1,...,6}, x axis line style=-]
\addplot+[ybar] plot coordinates {(1,16) (2,18) (3,23) (4,13) (5,16) (6,14)};
\end{axis}
\end{tikzpicture}

我明白了:

在此处输入图片描述

这不是我想要的,因为 4 的高度应该是 13。

尝试这个:

\begin{tikzpicture}
\begin{axis}[height=4.5cm, width=8cm, ybar, axis x line=bottom, axis y line=left, grid=none, ytick={0,10,20}, xmin=0, xmax=7, xtick={1,...,6}, x axis line style=-]
\addplot+[ybar] plot coordinates {(1,16) (2,18) (3,23) (4,13) (5,16) (6,0)};
\end{axis}
\end{tikzpicture}

给出这个:

在此处输入图片描述

我做错了什么吗?为什么我无法将条形图调整到正确的高度?

答案1

如果你包含 en yminymax它就会起作用。但不知道为什么。

\begin{tikzpicture}
    \begin{axis}[height=4.5cm, width=8cm, ybar, axis x line=bottom, axis y line=left, grid=none, ytick={0,10,20}, ymin=0, ymax=25, xmin=0, xmax=7, xtick={1,...,6}, x axis line style=-]
        \addplot+[ybar] coordinates {(1,16) (2,18) (3,23) (4,13) (5,16) (6,14)};
    \end{axis}
\end{tikzpicture}

在此处输入图片描述

答案2

此解决方案找到的另一种替代方法是使用 axis y line*=left,因为axis y line=left后者会修改 y 标签样式。请参阅此链接当轴 y 线=右时 ylabel 样式

在此处输入图片描述

代码

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
%\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[height=4.5cm, width=8cm, ybar, axis x line=bottom, axis y line*=left,grid=none, ytick={0,10,20}, xmin=0, xmax=7, xtick={1,...,6}, x axis line style=-]
\addplot+[ybar] plot coordinates {(1,16) (2,18) (3,23) (4,13) (5,16) (6,14)};
\end{axis}
\end{tikzpicture}

x axis line = none

\begin{tikzpicture}
\begin{axis}[height=4.5cm, width=8cm, ybar, axis x line=none, axis y line*=left,grid=none, ytick={0,10,20}, xmin=0, xmax=7, xtick={1,...,6}, x axis line style=-]
\addplot+[ybar] plot coordinates {(1,16) (2,18) (3,23) (4,13) (5,16) (6,0)};
\end{axis}
\end{tikzpicture}

x axis line = top

\begin{tikzpicture}
\begin{axis}[height=4.5cm, width=8cm, ybar, axis x line=top, axis y line*=left,grid=none, ytick={0,10,20}, xmin=0, xmax=7, xtick={1,...,6}, x axis line style=-]
\addplot+[ybar] plot coordinates {(1,16) (2,18) (3,23) (4,13) (5,16) (6,0)};
\end{axis}
\end{tikzpicture}

x axis line = bottom

\begin{tikzpicture}
\begin{axis}[height=4.5cm, width=8cm, ybar, axis x line=bottom, axis y line*=left,grid=none, ytick={0,10,20}, xmin=0, xmax=7, xtick={1,...,6}, x axis line style=-]
\addplot+[ybar] plot coordinates {(1,16) (2,18) (3,23) (4,13) (5,16) (6,0)};
\end{axis}
\end{tikzpicture}


\end{document}

相关内容