每条横线之间留出 1cm 的空白

每条横线之间留出 1cm 的空白

看看我制作直方图的以下作品,现在我想在每个条形之间留出一厘米的空白,需要进行哪些校正才能实现这一点?

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}

\begin{document}
    \begin{tikzpicture}
    \draw[<->] (-1cm,0cm) -- (13cm,0cm) node [right] {\large $x$};  %Abzisse

\draw[<->] (0cm,-1cm) -- (0cm,8cm) node [left] {\large $y$};  %Ordinate

\foreach \x in {1,...,7}
\draw[gray!50, text=black] (0 cm,\x cm) -- (13 cm,\x cm) 
    node at (-0.5 cm,\x cm) 
            {\the\numexpr\x*5};

\definecolor{myblue}{HTML}{92dcec}

\foreach \x / \y in {
    1 / 30,
    2 / 25,
    3 / 20,
    4 / 15,
    5 / 10,6 / 5} 
    {%
        \draw[fill=myblue] (\x,0) rectangle (\x + 1, \y/5 );
        \node at (\x+.5,\y/5  + .4) {\y};

};
 \node[rotate=0, below] at (3.5cm, -0.9cm) {$\longrightarrow$}; 

 \node[rotate=90, left] at (-1.3cm, 6cm) { $\longrightarrow$}; 

 \node[rotate=00, left] at (1.8cm, -0.5cm) {$1$};
 \node[rotate=00, left] at (2.8cm, -0.5cm) {$2$};
 \node[rotate=00, left] at (3.8cm, -0.5cm) {$3$};
 \node[rotate=00, left] at (4.8cm, -0.5cm) {$4$};
 \node[rotate=00, left] at (5.8cm, -0.5cm) {$5$};
 \node[rotate=00, left] at (6.8cm, -0.5cm) {$6$};


\end{tikzpicture}
\end{document}

答案1

我认为pgfplots它更适合绘制条形图。如果您对代码有任何疑问,请随时发表评论。

输出

示例图片

代码

\documentclass[margin=20pt]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.7}
\definecolor{myblue}{HTML}{92dcec}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar,
        x=2cm,
        bar width=1cm,
        axis lines=center,
        enlargelimits=false,
        enlarge x limits=false,
        xmin=0,xmax=7,
        ymin=0,ymax=40,
        ytick={5,10,15,20,25,30,35},
        xtick={1,2,3,4,5,6},
        nodes near coords,
        ymajorgrids,
        xlabel={$x$},
        ylabel={$y$},
        x label style={at={(axis description cs:1,0)}, anchor=west},
        y label style={at={(axis description cs:0,1)}, anchor=east},
        axis line style={shorten <=-1cm,<->},
        after end axis/.code={
            \node at (axis description cs:.5,-.15) {$\longrightarrow$};
            \node[rotate=90] at (axis description cs:-.1,.5) {$\longrightarrow$};
        }
        ]

        \addplot[fill=myblue] coordinates {(1,30) (2,25) (3,20) (4,15) (5,10) (6,5)};   
    \end{axis}
\end{tikzpicture}
\end{document}

答案2

这就是你要找的东西吗?

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
\draw[<->] (-1cm,0cm) -- (13cm,0cm) node [right] {\large $x$}; %Abzisse
\draw[<->] (0cm,-1cm) -- (0cm,8cm) node [left] {\large $y$};  %Ordinate
\foreach \x in {1,...,7}
    \draw[gray!50, text=black] (0 cm,\x cm) -- (13 cm,\x cm) node at (-0.5 cm,\x cm) {\the\numexpr\x*5};
\definecolor{myblue}{HTML}{92dcec}
\foreach \x / \y in {
1 / 30,
2 / 25,
3 / 20,
4 / 15,
5 / 10,
6 / 5
} 
{%
\draw[fill=myblue] (\x,0) rectangle (\x + 0.5, \y/5 );
\draw[fill=black] (\x + 0.5,0) rectangle (\x + 1, \y/5 );
\node at (\x+.25,\y/5  + .4) {\y};
\node at (\x+.25,-0.5) {\x};
};
\node[rotate=0, below] at (3.5cm, -0.9cm) {$\longrightarrow$}; 
\node[rotate=90, left] at (-1.3cm, 6cm) { $\longrightarrow$};

\end{tikzpicture}
\end{document}

相关内容