我怎样才能完成这个图表?

我怎样才能完成这个图表?

我正在尝试使用简单的 tikz 代码绘制直方图。这是我的代码

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{shapes.geometric,shapes.symbols,shapes.arrows,shapes.callouts}

\begin{document}
\begin{tikzpicture}[scale=1]
\draw [<->] (0,4) -- (0,0) -- (7,0);
\node at (-0.5,4) {$p(x)$};\node at (7,-0.5) {$x$};
\node at (0.25,-0.5) {\tiny{1}};
\node at (0.75,-0.5) {\tiny{1.5}};\node at (1.25,-0.5) {\tiny{2.0}}; \node at (1.75,-0.5) {\tiny{2.5}};\node at (2.25,-0.5) {\tiny{3.0}};\node at (2.75,-0.5) {\tiny{3.5}};\node at (3.25,-0.5) {\tiny{4.0}};\node at (3.75,-0.5) {\tiny{4.5}};
\node at (4.25,-0.5) {\tiny{5.0}};\node at (4.75,-0.5) {\tiny{5.5}};\node at (5.25,-0.5) {\tiny{6.0}};
\draw  (2,2) -- (2.5,2) rectangle(2.5,0);
\draw (1.5,1.7) -- (2,1.7) rectangle (2,0);
\draw  (1,1.4) -- (1.5,1.4) rectangle (1.5,0);
\draw  (0.5,1.1) -- (1,1.1) rectangle (1,0);
\draw  (0,0.8) -- (0.5,0.8)rectangle (0.5,0);
\draw  (3,0) -- (3,2.5) rectangle (2.5,2.5);
\draw  (3.5,0) -- (3.5,2)rectangle (3,2);
\draw  (4,0) --  (4,1.7)rectangle (3.5,1.7);
\draw  (4.5,0) -- (4.5,1.4) rectangle (4,1.4);
\draw  (5,0) -- (5,1.1)rectangle (4.5,1.1);
\draw (5.5,0) -- (5.5,0.8)rectangle (5,0.8);

\end{tikzpicture}
\end{document}

我的问题是画出的矩形不完整,你可以从下图中清楚地看到:

在此处输入图片描述

我需要一条出路。谢谢

答案1

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1]
\draw [<->] (0,4) -- (0,0) -- (7,0);
\draw(1,0) rectangle (1.5,1.4);
\draw(1.5,0) rectangle (2,1.7);
\draw(2,0) rectangle(2.5,2);


\end{tikzpicture}
\end{document}

您需要类似的东西 - 您的矩形语法完全混乱。我只修改了您的 3 个矩形 - 以同样的方式更改其他矩形。这里:

\draw(Lower Left Coordinate) rectangle(Upper right coordinate)

这里有一个更易于使用的带有宏的版本:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{calc}
\begin{tikzpicture}[scale=1]
\coordinate(O)at(0,0);
\coordinate(dX)at(0.5,0);
\newcommand{\RectangleforHamif}[1]{\draw(O)rectangle($(O)+(dX)+(0,#1)$);\coordinate(O) at ($(O)+(dX)$);}

\draw [<->] (0,4) -- (0,0) -- (7,0);
%"Plotting"
\foreach \i in {0.8,1.1,1.4,1.7,2}{\RectangleforHamif{\i}}
\end{tikzpicture}
\end{document}

从 (O) (=0,0) 开始,然后调用 \RectangleforHamif{Y-Value}

但事实上,整个事情应该按照情节来完成,而不是用“手”来绘制。

答案2

仅供比较,以下是使用 PGFPlots 执行此操作的方法:

\documentclass{article} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}


\begin{document}   
\begin{tikzpicture}
\begin{axis}[
        axis lines*=left,
        ybar,
        ymin=0,
        bar width=0.5,
        xlabel=$x$, ylabel=$y$,
        ylabel style={rotate=-90}
    ]
\addplot table {
1   0.8
1.5 1.1
2   1.4
2.5 1.7
3   2
3.5 2.5
4   2
4.5 1.7
5   1.4
5.5 1.1
6   0.8
};
\end{axis}
\end{tikzpicture}
\end{document}

答案3

不要画两次线。使用:

%....
    \draw  (2,2) rectangle(2.5,0);
    \draw (1.5,1.7)  rectangle (2,0);
    \draw  (1,1.4) rectangle (1.5,0);
    \draw  (0.5,1.1) rectangle (1,0);
    \draw  (0,0.8) rectangle (0.5,0);
    \draw  (3,0) rectangle (2.5,2.5);
    \draw  (3.5,0) rectangle (3,2);
    \draw  (4,0) rectangle (3.5,1.7);
    \draw  (4.5,0) rectangle (4,1.4);
    \draw  (5,0) rectangle (4.5,1.1);
    \draw (5.5,0) rectangle (5,0.8);
%....

相关内容