Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm using \symbolic y coords in a xbar-plot. Some of these coords are very long. There is a solution using nodes which is very ... hardcoded. And it would be nice to have it automated.

  1. How do I generate line breaks in ticks labels?
  2. Is there a way without using nodes?

MWE:

\documentclass[12pt,a4paper]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=0.9\textwidth,compat=newest}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={at={(0.5,1.025)}, anchor=south,semithick,legend columns=3}}
    \begin{axis}[   xlabel=x-label,
            xbar,
            xmin=0,
            enlarge y limits=0.2,
            height=0.5\textwidth,
            ytick=data,
            bar width=7pt,
            symbolic y coords={y label 1,y label 2,very very very very very long y label 3,thelongestylabeleverwrittenasoneword},]

            \addplot coordinates
            {(5.3987,y label 1)(16.1961,y label 2)(32.081,very very very very very long y label 3)(41.9026,thelongestylabeleverwrittenasoneword)};
            \addplot coordinates
            {(2.3917,y label 1)(7.1751,y label 2)(10.153,very very very very very long y label 3)(15.5834,thelongestylabeleverwrittenasoneword)};
            \addplot coordinates
            {(0.4881,y label 1)(1.4643,y label 2)(4.361,very very very very very long y label 3)(2.7435,thelongestylabeleverwrittenasoneword)};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this question
    
Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. Also, you don't have to sign with your name since it automatically appears in the lower right corner of your post. –  Joseph Wright Nov 23 '11 at 9:55
1  
That is not an MWE. It contains many irrelevant packages. Please only include packages that are relevant for the problem. –  N.N. Nov 23 '11 at 11:47

1 Answer 1

up vote 11 down vote accepted

I would approach this plot a bit differently: Instead of using symbolic y coords, I would manually specify the labels using yticklabels={<First>,<Second>,...}, which makes it possible to specify the line breaks explicitly by using something like yticklabels={Primär-\\energieverbrauch,...}. This will only work if you also specify an align for the labels, by setting yticklabel style={align=right}, for instance.

I would also load the data into a table first, by loading the pgfplotstable package (which ships with PGFplots) and then using \pgfplotstableread{<data table>}<macro name>. You can then say \addplot table [x=<column name>] <macro name> to plot a column. If your data table does not contain a numerical ID field that can be used for the y position, you can say \addplot table [x=<column name>, y=\coordindex] ..., which will sequentially assign a number to each row in the table.

\documentclass[12pt,a4paper]{article}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{width=0.9\textwidth,compat=1.4}

\pgfplotstableread[col sep=comma]{
Name,A,B,C
Stromverbrauch, 5398.7, 2391.7, 488.1
Primärenergieverbrauch, 16196.1, 7175.1, 1464.3
Primärenergieeinsparung, 10153, 15583.4, 4361
Kälteproduktion, 32081, 41902.6, 2743.5
}\datatable
\begin{document}

\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={at={(0.5,1.025)}, anchor=south,semithick,legend columns=3}}
    \begin{axis}[   xlabel=Energie {[kWh]},
            xbar,
            xmin=0,
            enlarge y limits=0.2,
            height=0.5\textwidth,
            ytick=data,
            yticklabels={
                Stromverbrauch,
                Primärenergie-\\verbrauch,
                Primärenergie-\\einsparung,
                Kälteproduktion
            },
            yticklabel style={align=right},
            bar width=7pt,
            ]

            \addplot [fill=red] table [x=A,y expr=\coordindex] {\datatable};
            \addplot [fill=green] table [x=B,y expr=\coordindex] {\datatable};
            \addplot [fill=blue] table [x=C,y expr=\coordindex] {\datatable};
    \end{axis}
\end{tikzpicture}

\end{document}

Generally, you can set the maximum width the labels may take up by saying yticklabel style={text width=<length>}, forcing the text to wrap around. It seems, however, that this doesn't work with umlauts, even if you load [T1]{fontenc}. Here's an example that compiles correctly with lualatex instead of pdflatex, replacing the inputenc and fontenc packages with fontspec:

\documentclass[12pt,a4paper]{article}
\usepackage{lmodern}
\usepackage{fontspec}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\pgfplotsset{width=0.9\textwidth,compat=1.4}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{every axis legend/.append style={at={(0.5,1.025)}, anchor=south,semithick,legend columns=3}}
    \begin{axis}[   xlabel=Energie {[kWh]},
            xbar,
            xmin=0,
            enlarge y limits=0.2,
            height=0.5\textwidth,
            ytick=data,yticklabel style={text width=3cm,align=right},
            bar width=7pt,
            symbolic y coords={Stromverbrauch,Primärenergieverbrauch,Primärenergieeinsparung,Kälteproduktion},]

            \addplot [fill=red] coordinates
            {(5398.7,Stromverbrauch)(16196.1,Primärenergieverbrauch)(32081,Primärenergieeinsparung)(41902.6,Kälteproduktion)};
            \addplot [fill=green] coordinates
            {(2391.7,Stromverbrauch)(7175.1,Primärenergieverbrauch)(10153,Primärenergieeinsparung)(15583.4,Kälteproduktion)};
            \addplot [fill=blue] coordinates
            {(488.1,Stromverbrauch)(1464.3,Primärenergieverbrauch)(4361,Primärenergieeinsparung)(2743.5,Kälteproduktion)};
    \end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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