1

This is a follow up question to my first steps with pgf. The chart is now somehow how I want it to be, but it doesn't look perfect yet. I tried to figure it out with the manual, but well...

  • Is it possible to include a line break in the labels, that are to long (maybe automatic)?
  • Is formatting of the text possible? When I used textbf, I got errors
  • How to change the colour of each bar. The only solution I found, needs to remove the text labels

Thanks all you tikz experts in advance :)

\documentclass[border=10pt]{standalone}
\usepackage{verbatim}
\usepackage{pgfplots}
\pgfplotsset{width=7cm, compat=1.12}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    ybar stacked,
    width=0.7\textwidth,
    height=0.7\textwidth,
    bar width=.5\textwidth,
    point meta=explicit symbolic,
    nodes near coords,
    enlargelimits=0,
    ylabel={Spannung in V},
    symbolic x coords={tool1},
    xtick=data,
    xtick style={draw=none},
    xticklabel=\empty,
    ymin=0,
    ymax=70,
    ]
    \addplot+[ybar] plot coordinates {(tool1,20) [thisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,4) [thisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,12) [thisisalongword with anotherthisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,16) [thisisalongword with anotherthisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,2) [thisisalongword with anotherthisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,4) [andagain thisisalongerword]};
    \addplot+[ybar] plot coordinates {(tool1,2) [thisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,10) [andagain thisisalongerword]};         
    \end{axis}
    \end{tikzpicture}
\end{document}

3
  • got an error, when compiled.
    – Zam
    Sep 27 '16 at 11:39
  • @SamjithDasan It works for me! (See also:overleaf.com/read/nsbkzsbkrwgg)
    – Marcel
    Sep 27 '16 at 12:12
  • Oh! something went wrong from my side.
    – Zam
    Sep 27 '16 at 12:28
1
  • Using every node near coord/.style={text width=3 cm,}, you can set the width of a text node.
  • Add fill=color to \addplot such as: \addplot+[ybar, fill=red] plot.... to change the color.
  • You can make the font bold via every node near coord/.style={font=\bfseries},

You can also change much more via every node near coord/.style. Such as if you don't like that the text is aligned on the left, but you want it centered, then add align=center.

MWE

\documentclass[border=10pt]{standalone}
\usepackage{verbatim}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    ybar stacked,
    width=0.7\textwidth,
    height=0.7\textwidth,
    bar width=.5\textwidth,
    point meta=explicit symbolic,
    nodes near coords,
    enlargelimits=0,
    ylabel={Spannung in V},
    symbolic x coords={tool1},
    xtick=data,
    xtick style={draw=none},
    xticklabel=\empty,
    ymin=0,
    ymax=70,
    every node near coord/.style={text width=3 cm, font=\bfseries, align=center},
    ]
    \addplot+[ybar, fill=red] plot coordinates {(tool1,20) [thisisalongword]};
    \addplot+[ybar, fill=blue] plot coordinates {(tool1,4) [thisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,12) [thisisalongword with anotherthisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,16) [thisisalongword with anotherthisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,2) [thisisalongword with anotherthisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,4) [andagain thisisalongerword]};
    \addplot+[ybar] plot coordinates {(tool1,2) [thisisalongword]};
    \addplot+[ybar] plot coordinates {(tool1,10) [andagain thisisalongerword]};         
    \end{axis}
    \end{tikzpicture}
\end{document}

Result

enter image description here

4
  • Thank you very much! I thing \bfseries should be used insetad of \bf. Otherwise Latex throws errors (with Komascript)
    – Marcel
    Sep 28 '16 at 6:10
  • I tried to put the fill comand in the wrong bracket... I guess it will need some time, till I get used to tikz and pgf
    – Marcel
    Sep 28 '16 at 6:12
  • 1
    @Marcel I changed it. I looked it up and \bf is depreciated an use is discouraged. Thanks for the reply!
    – Roald
    Sep 28 '16 at 6:13
  • I have to thank you. (I changed it in your MWE as well) If someone in the future finds this topic: font=\small\bfseries works as well. And with draw=none next to the fill command, the colourful borders are eliminated :)
    – Marcel
    Sep 28 '16 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.