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.

Please consider the following MWE. The same code is in another document with a larger preamble, so maybe my problem with the misaligned xticklabels is because of conflicting packages.

The first screenshot shows the results of the MWE and the second how it looks like when compiled with a larger preamble.

As is should be Misaligned <code>xticklabels</code>

Note: For the MWE I did not include special formatting (e.g. colours, dashing etc).

EDIT By the way: If you take a look at the data in dataLevelerConverter2.dat you will see that there is a Kalenderwoche 42. However, in the first screenshot it seems that the xticks start with Kalenderwoche 43.

MWE

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots} 
\usepgfplotslibrary{groupplots}


\begin{filecontents}{dataLevelerConverter2.dat}
Wert    Kalenderwochen  Nachfrage   Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1   42  nan nan nan nan nan 128 608
2   43  255 255 nan nan nan 1088    608
3   44  1584    1584    1584    nan nan 960 1265
4   45  1296    nan 1296    1296    nan 1570    1265
5   46  432 nan nan 432 nan 610 610
6   47  972 nan nan 972 nan 610 610
7   48  540 nan nan 540 nan 610 610
8   49  1104    nan nan 1104    1104    610 610
9   50  0   nan nan nan 0   0   0
\end{filecontents}


%%%%Some number formatting for pgfplots%%%%
\pgfkeys{/pgf/number format/.cd,%additional code
      use comma,%additional code
       1000 sep={\,},%additional code
}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        width=10cm,
        height=6cm,
        xticklabels from table = {dataLevelerConverter2.dat}{Kalenderwochen},
        xtick from table = {dataLevelerConverter2.dat}{Wert},
%        xtick = {1,...,9},
%        xtick = data,
        xlabel=Kalenderwoche,
        ylabel=Kundennachfrage,
        legend style={at={(0.5,-0.3)},anchor=north,draw=none},
        legend columns=3
]

    \addplot table [x=Wert, y=Nachfrage] {dataLevelerConverter2.dat} ;
        \addlegendentry{label1}

    \addplot table [x=Wert, y=nachLeveler] {dataLevelerConverter2.dat} ;
        \addlegendentry{label2}

    \addplot table [x=Wert, y=nachConverter] {dataLevelerConverter2.dat} ;
        \addlegendentry{label3}

    \end{axis}
\end{tikzpicture}

\end{document}
share|improve this question
    
You are probably supplying the tick labels manually and the remaning ones are left empty because it runs out of tick labels on the list. Without any other info we can't say much. –  percusse Oct 30 '12 at 17:07
    
@percusse: The code for the plot is for both, the MWE and the larger tex-document the same. I do not supply any special information for the tick labels. (Hope I understood you correctly) –  John Oct 30 '12 at 17:09
    
@John: Thanks for putting together a complete compilable example. The only thing missing is that you need to include enough of the preamble so that the MWE here reproduce the problem. Not sure how we can possibly guess what other settings are causing the problem. –  Peter Grill Oct 30 '12 at 17:18
1  
You don't need to read the xticklabels from the table. Just set x=Kalenderwochen in the addplot options. If you need more ticks, add try min ticks=9 in the axis options. This will change the yticks too, so you can set ytick={0,500,...,1500} if you want to preserve the yticks. –  Luigi Oct 30 '12 at 19:37
1  
@Luigi: "Kalenderwochen" translates to "calendar weeks", so these will wrap around, so just using x=Kalenderwochen might work for this concrete example, but is not the "proper" thing to do for this kind of data. –  Jake Oct 30 '12 at 20:08
show 4 more comments

2 Answers 2

up vote 3 down vote accepted

Luigi's suggestion of using Kalenderwochen as the x-coordinate directly is a good idea in this concrete example. However, it won't work if Kalenderwochen indeed contains labels (numeric or otherwise): In John's text label example, the xtick={0,...,9} has to be set explicitly. Usually, one could just use xtick=data, but that only works if there are no nans in the first plot.

So, for the rare case where you need to provide labels from a table but can't use xtick=data to make sure the ticks and ticklabels are synchronised, here's a style xtick from table that works analogously to xticklabels from data:


This approach has the drawback that you're always using all ticks and ticklabels, instead of letting PGFPlots determine the appropriate number of ticks. To fix this, you can set

    xticklabel={%
        \pgfmathtruncatemacro\datarow{round(\tick)}%
        \pgfplotstablegetelem{\datarow}{Kalenderwochen}\of{dataLevelerConverter2.dat}%
        \pgfplotsretval%
    }

which will get the appropriate tick label for each tick position from the data table.


Code for the first example

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{
    xticks from table/.code 2 args={%
        \pgfplotstablegetrowsof{#1}
        \pgfmathsetmacro\pgfplotstabletotalrows{\pgfplotsretval-1}
        \edef\ticklist{}
            \pgfplotstableforeachcolumnelement{#2}\of#1\as\cell{%
                \edef\ticklist{\ticklist\cell\ifnum\pgfplotstablerow<\pgfplotstabletotalrows,\fi}
            }
        \pgfplotsset{xtick=\ticklist}
    },
}


\begin{filecontents}{dataLevelerConverter2.dat}
Wert    Kalenderwochen  Nachfrage   Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1   42  nan nan nan nan nan 128 608
2   43  255 255 nan nan nan 1088    608
3   44  1584    1584    1584    nan nan 960 1265
4   45  1296    nan 1296    1296    nan 1570    1265
5   46  432 nan nan 432 nan 610 610
6   47  972 nan nan 972 nan 610 610
7   48  540 nan nan 540 nan 610 610
8   49  1104    nan nan 1104    1104    610 610
9   50  0   nan nan nan 0   0   0
10   51  255 255 nan nan nan 1088    608
11   52  1584    1584    1584    nan nan 960 1265
12   1  1296    nan 1296    1296    nan 1570    1265
\end{filecontents}


%%%%Some number formatting for pgfplots%%%%
\pgfkeys{/pgf/number format/.cd,%additional code
      use comma,%additional code
       1000 sep={\,},%additional code
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=10cm,
        height=6cm,
        xticklabels from table = {dataLevelerConverter2.dat}{Kalenderwochen},
        xticks from table = {dataLevelerConverter2.dat}{Wert},
        xlabel=Kalenderwoche,
        ylabel=Kundennachfrage,
        legend style={at={(0.5,-0.3)},anchor=north,draw=none},
        legend columns=3
]

    \addplot table [x=Wert, y=Nachfrage] {dataLevelerConverter2.dat} ;
        \addlegendentry{label1}

    \addplot table [x=Wert, y=nachLeveler] {dataLevelerConverter2.dat} ;
        \addlegendentry{label2}

    \addplot table [x=Wert, y=nachConverter] {dataLevelerConverter2.dat} ;
        \addlegendentry{label3}

    \end{axis}
\end{tikzpicture}

\end{document}

Code for the second example:

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{
    xticks from table/.code 2 args={%
        \pgfplotstablegetrowsof{#1}
        \pgfmathsetmacro\pgfplotstabletotalrows{\pgfplotsretval-1}
        \edef\ticklist{}
            \pgfplotstableforeachcolumnelement{#2}\of#1\as\cell{%
                \edef\ticklist{\ticklist\cell\ifnum\pgfplotstablerow<\pgfplotstabletotalrows,\fi}
            }
        \pgfplotsset{xtick=\ticklist}
    },
}


\begin{filecontents}{dataLevelerConverter2.dat}
Wert    Kalenderwochen  Nachfrage   Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1   42  nan nan nan nan nan 128 608
2   43  255 255 nan nan nan 1088    608
3   44  1584    1584    1584    nan nan 960 1265
4   45  1296    nan 1296    1296    nan 1570    1265
5   46  432 nan nan 432 nan 610 610
6   47  972 nan nan 972 nan 610 610
7   48  540 nan nan 540 nan 610 610
8   49  1104    nan nan 1104    1104    610 610
9   50  0   nan nan nan 0   0   0
10   51  255 255 nan nan nan 1088    608
11   52  1584    1584    1584    nan nan 960 1265
12   1  1296    nan 1296    1296    nan 1570    1265
\end{filecontents}


%%%%Some number formatting for pgfplots%%%%
\pgfkeys{/pgf/number format/.cd,%additional code
      use comma,%additional code
       1000 sep={\,},%additional code
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=6cm,
        height=6cm,
        enlargelimits=false,
        xticklabel={%
            \pgfmathtruncatemacro\datarow{round(\tick)}%
            \pgfplotstablegetelem{\datarow}{Kalenderwochen}\of{dataLevelerConverter2.dat}%
            \pgfplotsretval%
        },
        xlabel=Kalenderwoche,
        ylabel=Kundennachfrage,
        legend style={at={(0.5,-0.3)},anchor=north,draw=none},
        legend columns=3
]

    \addplot table [x expr=\coordindex, y=Nachfrage] {dataLevelerConverter2.dat} ;
        \addlegendentry{label1}

    \addplot table [x expr=\coordindex, y=nachLeveler] {dataLevelerConverter2.dat} ;
        \addlegendentry{label2}

    \addplot table [x expr=\coordindex, y=nachConverter] {dataLevelerConverter2.dat} ;
        \addlegendentry{label3}

    \end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
+1 very sharp analysis and solution. –  Luigi Oct 31 '12 at 8:46
    
@Jake There is one problem with your approach. If you lower the width to, for instance 6cm, the tick labels get to close/overlap. How must one change your code so that LaTeX only plots such a number of tick labels so that they do not overlap. (e.g. with width=6cm it would be better to have only {42,44,...,1} as ticklabels. –  John Nov 27 '12 at 16:28
1  
@John: Fair point. I've added a different approach that allows you to use PGFPlots normal tick placement algorithm or to manually select which ticks are placed. –  Jake Nov 27 '12 at 16:45
add comment

@Luigi and @Jake are right.

xticklabels are numbers

I wanted to use the column Kalenderwochen in dataLevelerConverter2.dat as xticklabels. As this column contains numbers I can write

\addplot table [x=Kalenderwochen, y=Nachfrage]

instead of

\addplot table [x=Wert, y=Nachfrage]

and delete

xticklabels from table = {dataLevelerConverter2.dat}{Kalenderwochen}

xticklabels are not numbers

In the case the column Kalenderwochen contains other information than numbers (e.g. "first data set", "second data set") one must write

\addplot table [x=Wert, y=Nachfrage]
xticklabels from table = {dataLevelerConverter2.dat}{Kalenderwochen}

enter image description here

MWE

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots} 
\usepgfplotslibrary{groupplots}


\begin{filecontents}{Numbers.dat}
Wert    Kalenderwochen  Nachfrage   Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1   42  nan nan nan nan nan 128 608
2   43  255 255 nan nan nan 1088    608
3   44  1584    1584    1584    nan nan 960 1265
4   45  1296    nan 1296    1296    nan 1570    1265
5   46  432 nan nan 432 nan 610 610
6   47  972 nan nan 972 nan 610 610
7   48  540 nan nan 540 nan 610 610
8   49  1104    nan nan 1104    1104    610 610
9   50  0   nan nan nan 0   0   0
\end{filecontents}

\begin{filecontents}{Strings.dat}
Wert    Kalenderwochen  Nachfrage   Nachfrage1a Nachfrage1b Nachfrage1c Nachfrage1d nachLeveler nachConverter
1   first  nan nan nan nan nan 128 608
2   second  255 255 nan nan nan 1088    608
3   third  1584    1584    1584    nan nan 960 1265
4   fourth  1296    nan 1296    1296    nan 1570    1265
5   fifth  432 nan nan 432 nan 610 610
6   sixth  972 nan nan 972 nan 610 610
7   seventh  540 nan nan 540 nan 610 610
8   eighth  1104    nan nan 1104    1104    610 610
9   ninth  0   nan nan nan 0   0   0
\end{filecontents}

%%%%Some number formatting for pgfplots%%%%
\pgfkeys{/pgf/number format/.cd,%additional code
      use comma,%additional code
       1000 sep={\,},%additional code
}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        width=10cm,
        height=6cm,
        xlabel=Kalenderwoche,
        ylabel=Kundennachfrage,
        legend style={at={(0.5,-0.3)},anchor=north,draw=none},
        legend columns=3
]

    \addplot table [x=Kalenderwochen, y=Nachfrage] {Numbers.dat} ;
        \addlegendentry{label1}

    \addplot table [x=Kalenderwochen, y=nachLeveler] {Numbers.dat} ;
        \addlegendentry{label2}

    \addplot table [x=Kalenderwochen, y=nachConverter] {Numbers.dat} ;
        \addlegendentry{label3}

    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        width=10cm,
        height=6cm,
        xticklabels from table = {Strings.dat}{Kalenderwochen},
        xtick = {1,...,9},
        xticklabel style={rotate=90},
        xlabel=Kalenderwoche,
        xlabel style={at={(ticklabel cs:0.5)},anchor=near ticklabel},
        ylabel=Kundennachfrage,
        legend style={at={(0.5,-0.3)},anchor=north,draw=none},
        legend columns=3
]

    \addplot table [x=Wert, y=Nachfrage] {Strings.dat} ;
        \addlegendentry{label1}

    \addplot table [x=Wert, y=nachLeveler] {Strings.dat} ;
        \addlegendentry{label2}

    \addplot table [x=Wert, y=nachConverter] {Strings.dat} ;
        \addlegendentry{label3}

    \end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
1  
Did you solve the problem in your question? By the way, you don't need the first column in your table: you can simply use x expr=\coordindex in the addplot options, along with xtick={0,...,8} (you have to load the pgfplotstable package too). –  Luigi Oct 31 '12 at 8:38
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.