我使用以下代码创建了我的图表。
\documentclass{article}
%
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols,patterns}
%
\pagestyle{empty}
%
\begin{document}
%
\tikzset{
bar cross/.style={rectangle, pattern=north east lines, minimum height=50mm, minimum width=10mm, draw=black,
anchor=north},
bar/.style={rectangle, minimum height=50mm, minimum width=10mm, draw=black,anchor=north}
}
%
\begin{tikzpicture}
\node(01)[bar,minimum height=30mm]at(0.0,0){};
\node[above]at(01.north){$m_1$};
\node(011)[bar cross,minimum height=20mm]at(0.0,-3){};
\node(02)[bar,minimum height=5mm]at(1.5,0){};
\node[above]at(02.north){$m_1$};
\node(021)[bar cross,minimum height=10mm]at(1.5,-0.5){};
\node(022)[bar,minimum height=5mm]at(1.5,-1.5){};
\node(023)[bar cross,minimum height=15mm]at(1.5,-2.0){};
\node(024)[bar,minimum height=15mm]at(1.5,-3.5){};
\node(03)[bar cross,minimum height=2.5mm]at(3.0,0){};
\node[above]at(03.north){$m_3$};
\node(031)[bar,minimum height=25mm]at(3.0,-0.25){};
\node(031)[bar cross,minimum height=22.5mm]at(3.0,-2.75){};
\node(04)[bar,minimum height=32mm]at(4.5,0){};
\node[above]at(04.north){$m_4$};
\node(041)[bar cross,minimum height=2.5mm]at(4.5,-3.2){};
\node(042)[bar,minimum height=15.5mm]at(4.5,-3.45){};
\node(05)[bar cross,minimum height=10mm]at(6.0,0){};
\node[above]at(05.north){$m_5$};
\node(055)[bar,minimum height=40mm]at(6.0,-1){};
\node(beob)[bar,minimum height=5mm]at(7.5,-2.5){};
\node[right]at(beob.east){type 1};
\node(fehl)[bar cross,minimum height=5mm]at(7.5,-3.5){};
\node[right]at(fehl.east){type 2};
\draw[->](-5,-5) -- (2.5,-2.75);
\end{tikzpicture}
%
\end{document}
%
我认为,矩形重叠处的线条看起来很粗,这是正确的还是只是我的看法?箭头显示的是这样的线条。
第二个问题是,我如何改进我的图表,我是tikz
初学者,我认为代码可以改进。
答案1
将其视为展示,如果你不熟悉的话,这确实有点太多了pgfplots
。但只要提供堆积条形图坐标,你就可以让它为你构建图形(我只是快速地目测了坐标)
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usetikzlibrary{patterns}
%
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
bar width=1cm,
stacked ignores zero,
ymin=0,xmin=0,
ymax=6,xmax=6,
enlargelimits=false,
area legend,
legend entries={Type 1,Type 2},
legend style={at={(axis description cs:1.0,0.5)},anchor=west},
axis lines=none,
]
\addplot[] coordinates {(1,0) (2,1.5) (3,0) (4,1.5) (5,4)};
\addplot[pattern=north west lines] coordinates {(1,2) (2,1.5) (3,2.2) (4,0.2) (5,1)};
\addplot[] coordinates {(1,3) (2,0.5) (3,2.5) (4,3.3) (5,0)};
\addplot[pattern=north west lines] coordinates {(1,0) (2,1) (3,0.3) (4,0) (5,0)};
\addplot[] coordinates {(1,0) (2,0.5) (3,0) (4,0) (5,0)};
\pgfplotsinvokeforeach{1,...,5}{
\node[above] (a#1) at (axis cs:#1,5){$m_#1$};
}
\end{axis}
\end{tikzpicture}
\end{document}
还有很多其他细节可以纳入其中,但我不想混淆主要思想。