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 (like "jährlicher Primärenergieverbrauch") very long.

so my questions are:

  1. how do I generate line breaks in coords?
  2. is there a way without using nodes? (something like a code I just missed)

first of all the MWE

\documentclass[12pt,a4paper]{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\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,
            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}

and here are the pictures

this is what it looks like

pic 1

and this is what it SHOULD look like

pic 2

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
add comment

1 Answer

up vote 8 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
    
Wow, it works very good. Thank you! –  Lorzen Nov 23 '11 at 12:37
add comment

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.