Seite 1 von 1

Stacked Bar mit Zusatzlinien

Verfasst: Sa 27. Mai 2017, 22:17
von tekken01
Sehr verehrtes Forum,

wie kann ich zusätzliche Striche in meinem Balkendiagram einzeichnen ? Ich hätte es gerne genauso wie hier:

Bild

ich dachte dass es möglicherweise über den draw Befehl ginge... :? Gibt es dazu eine elegante Lösung ?

hier mein minimalbeispiel:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
bar width=15pt,
nodes near coords,
enlargelimits=0.15,
legend style={at={(0.5,-0.20)},
	anchor=north,legend columns=-1},
symbolic x coords={tool1, tool2},
xtick=data,
x tick label style={rotate=45,anchor=east},
]
%\draw[red, very thick] (axis cs:0.7-\,2) -- (axis cs:1.3+\,2);

\addplot+[ybar] plot coordinates {(tool1,0) (tool2,2) 	};
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,0) 
	};
\addplot+[ybar] plot coordinates {(tool1,6) (tool2,6)	};
\addplot+[ybar] plot coordinates {(tool1,4) (tool2,2)};
\end{axis}
\end{tikzpicture}
\end{document}

Verfasst: Mo 29. Mai 2017, 12:52
von esdd
Vielleicht geht das auch irgendwie eleganter, aber hier ist zumindest mal ein Vorschlag:
\documentclass[tikz]{standalone} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.10} 
\begin{document} 
\begin{tikzpicture}
  \begin{axis}[ 
      ybar stacked, 
      bar width=15pt, 
      nodes near coords, 
      enlargelimits=0.15, 
      legend style={at={(0.5,-0.20)}, 
        anchor=north,legend columns=-1}, 
      symbolic x coords={tool1, tool2}, 
      xtick=data, 
      x tick label style={rotate=45,anchor=east},
      ymajorgrids,
      /tikz/mstyle/.style={draw,red,very thick,text=black},
      /tikz/mleft/.style={xshift={-(.5*\pgfplotbarwidth+2.5pt)}},
      /tikz/mline/.style={insert path={--+({\pgfplotbarwidth+5pt},0pt)}},
    ]
    \path[mstyle]([mleft]axis cs:tool1,2)[mline]node[right]{$2$};
    \path[mstyle]([mleft]axis cs:tool2,3)[mline]node[right]{$3$};
    
    \addplot plot coordinates {(tool1,0) (tool2,2)}; 
    \addplot plot coordinates {(tool1,0) (tool2,0)}; 
    \addplot plot coordinates {(tool1,6) (tool2,6)}; 
    \addplot plot coordinates {(tool1,4) (tool2,2)};

  \end{axis} 
\end{tikzpicture} 
\end{document}
Bild

Beachte, dass Du beim Zeichnen der Striche die symbolischen x-Koordinaten verwenden musst. Ist es Absicht, dass Du Kompatibilität zu der recht alten Version 1.10 herstellst? Aktuell ist derzeit 1.14