2

Sub-compilations of MWE

I will later create a final file which will show the problem (cf. Main compilation of MWE below), which will need the following input files:

  1. test1.pdf, which can be compiled using the following code (when you save the code as test1.tex)

    \documentclass{standalone}
    
    \usepackage{pgfplots}
    \pgfplotsset{width=67cm,compat=1.8}
    
    \pgfkeys{
    /pgf/number format/precision=0, 
    /pgf/number format/fixed zerofill=true,
    /pgf/number format/fixed
    }
    
    \usepackage{pgfplotstable}
    \renewcommand*{\familydefault}{\sfdefault}
    \usepackage{sfmath}
    
    \begin{document}
    \begin{tikzpicture}
    
    \centering
    \begin{axis}[yticklabel style={
    /pgf/number format/fixed,
    /pgf/number format/precision=0
    },
    scaled y ticks=false,
    ybar, axis on top,
    ticklabel style = {font=\Huge},
    enlarge x limits  = 0.04,
    height=15cm, width=67cm,
    bar width=3.5cm,
    ymajorgrids, tick align=inside,
    major grid style={draw=white},
    enlarge y limits= 0.2,
    ymin=-15, ymax=18,
    axis x line*=bottom,
    axis y line*=right,
    y axis line style={opacity=0},
    tickwidth=0pt,
    symbolic x coords={1998, 1999, 2000},
    xtick=data,
    nodes near coords={
    \pgfmathprintnumber[precision=2]{\pgfplotspointmeta}
    },
    every node near coord/.append style={font=\Huge},
    ]
    
    \addplot [draw=none,fill=yellow!100] coordinates {
    (1998, 8)
    (1999, 16) 
    (2000, 3)};
    
    \end{axis}
    \end{tikzpicture}
    
    \end{document}
    
  2. test2.pdf, which can be compiled using the following code (when you save the code as test2.tex)

    \documentclass{standalone}
    
    \usepackage{pgfplots}
    \pgfplotsset{width=67cm,compat=1.8}
    
    \pgfkeys{
        /pgf/number format/precision=0, 
        /pgf/number format/fixed zerofill=true,
        /pgf/number format/fixed
    }
    
    \usepackage{pgfplotstable}
    \renewcommand*{\familydefault}{\sfdefault}
    \usepackage{sfmath}
    
    \begin{document}
        \begin{tikzpicture}
    
        \centering
        \begin{axis}[yticklabel style={
            /pgf/number format/fixed,
            /pgf/number format/precision=0
        },
        scaled y ticks=false,
        ybar, axis on top,
        ticklabel style = {font=\Huge},
        enlarge x limits  = 0.04,
        height=15cm, width=67cm,
        bar width=3.5cm,
        ymajorgrids, tick align=inside,
        major grid style={draw=white},
        enlarge y limits= 0.2,
        ymin=-13000, ymax=14500,
        axis x line*=bottom,
        axis y line*=right,
        y axis line style={opacity=0},
        tickwidth=0pt,
        symbolic x coords={1998, 1999, 2000},
        xtick=data,
        nodes near coords={
            \pgfmathprintnumber[precision=2]{\pgfplotspointmeta}
        },
        every node near coord/.append style={font=\Huge},
        ]
    
        \addplot [draw=none,fill=yellow!100] coordinates {
            (1998, 3426)
            (1999, 4953) 
            (2000, 1505)};
        \addplot [draw=none, fill=green!100] coordinates {
            (1998, 2297)
            (1999, 4822) 
            (2000, 921)};
    
        \end{axis}
        \end{tikzpicture}
    
    \end{document}
    
  3. test3.pdf, which can be compiled using the following code (when you save the code as test3.tex)

    \documentclass{standalone}
    
    \usepackage{pgfplots}
    \pgfplotsset{width=67cm,compat=1.8}
    
    \pgfkeys{
        /pgf/number format/precision=0, 
        /pgf/number format/fixed zerofill=true,
        /pgf/number format/fixed
    }
    
    \usepackage{pgfplotstable}
    \renewcommand*{\familydefault}{\sfdefault}
    \usepackage{sfmath}
    
    \begin{document}
        \begin{tikzpicture}
    
        \centering
        \begin{axis}[yticklabel style={
            /pgf/number format/fixed,
            /pgf/number format/precision=0
        },
        scaled y ticks=false,
        ybar, axis on top,
        ticklabel style = {font=\Huge},
        enlarge x limits  = 0.04,
        height=15cm, width=67cm,
        bar width=3.5cm,
        ymajorgrids, tick align=inside,
        major grid style={draw=white},
        enlarge y limits= 0.2,
        ymin=40000, ymax=270000,
        axis x line*=bottom,
        axis y line*=right,
        y axis line style={opacity=0},
        tickwidth=0pt,
        symbolic x coords={1998, 1999, 2000},
        xtick=data,
        nodes near coords={
            \pgfmathprintnumber[precision=2]{\pgfplotspointmeta}
        },
        every node near coord/.append style={font=\Huge},
        ]
    
        \addplot [draw=none,fill=yellow!100] coordinates {
            (1998, 69939)
            (1999, 55986) 
            (2000, 51422)};
        \addplot [draw=none, fill=red!100] coordinates {
            (1998, 74916)
            (1999, 77029) 
            (2000, 89716)};
        \addplot [draw=none,fill=green!100] coordinates {
            (1998, 46452)
            (1999, 51274) 
            (2000, 53092)};
    
        \end{axis}
        \end{tikzpicture}
    
    \end{document}
    

Main compilation of MWE

\documentclass{article}
\usepackage[a4paper,margin=2cm]{geometry}

\usepackage{graphicx}

\begin{document}

\begin{figure}[h]
\centering
\includegraphics[width=1.0\textwidth]{test1.pdf}
\end{figure}

\begin{figure}[h]
\centering
\includegraphics[width=1.0\textwidth]{test2.pdf}
\end{figure}

\begin{figure}[h]
\centering
\includegraphics[width=1.0\textwidth]{test3.pdf}
\end{figure}

\end{document}

That yields something as such (without the blue-ish lines):

Main compilation of MWE, indication of problem by pink lines

The blue-ish lines indicate the problem, namely that the dates (or values on the x-axis), e.g. the three instances of 1998, are not lined up vertically.

My question is: how to make them line up vertically, please?

Some comments

I think one of the issues at stake (from elaborate tweaking in a more detailed parent problem) are the values of ymin=... and ymax=.... If those values are very big, or very small, it makes a difference as with regards to the vertical line-up of the different x-axes.

1
  • 1
    off-topic: fyi, to format code within a list (bullet points), you need to indent your code with 4 (list item formating) + 4 (code formating) = 8 spaces.
    – ebosi
    Apr 18, 2016 at 12:09

1 Answer 1

1

I think the issue is caused by the different lengths of the yticklabels, combined with the node near coord that sticks out on the left side of the last plot. The last issue can be fixed by increasing the value for enlarge x limits to a suitable value. I think you figured this out as well. The issue with the different tick widths can be circumvented by adding an extra tick, and setting the label of that to an empty box, that is the width of the widest tick in plot, or wider. To do that, I've added

extra y ticks={10},extra y tick labels={\hphantom{$300,000$}},

to all three axis environments. I also set

enlarge x limits=0.1

as mentioned above. With only those two changes to your original code, I get this:

enter image description here

Old answer

Can't you use just one tikzpicture, and a groupplot environment? Personally I would also prefer setting the width to a useful size, and reducing font size, rather than making a huge PDF and scaling that down.

For example, the code below produces a PDF that should just about fit in an article document with margin=2cm. I've tweaked the height, width, and enlarge x limits parameters a bit.

%\documentclass{article}
%\usepackage[a4paper,margin=2cm,showframe]{geometry} %showframe options indicates margins

\documentclass{standalone}
\setlength\textwidth{483.69pt} % comes from using \the\textwidth in a document compiled with the article class and geometry package as above
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots} %added
\pgfkeys{
/pgf/number format/precision=0, 
/pgf/number format/fixed zerofill=true,
/pgf/number format/fixed
}

\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}

\begin{document}
%\the\textwidth
\begin{tikzpicture}
\begin{groupplot}[
group style={group size=1 by 3},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/precision=0
},
scaled y ticks=false,
ybar, axis on top,
%ticklabel style = {font=\Huge},
enlarge x limits  = 0.15,
height=0.25\textwidth, width=\textwidth,
/tikz/bar width=0.05\textwidth,
ymajorgrids, tick align=inside,
major grid style={draw=white},
enlarge y limits= 0.2,
axis x line*=bottom,clip=false,
axis y line*=right,
y axis line style={opacity=0},
tickwidth=0pt,
symbolic x coords={1998, 1999, 2000},
xtick=data,
nodes near coords={
\pgfmathprintnumber[precision=2]{\pgfplotspointmeta}
},
every node near coord/.append style={font=\tiny},
]
\nextgroupplot[ymin=-15, ymax=18,ytick={-15,-10,...,20}]
\addplot [draw=none,fill=yellow!100] coordinates {
(1998, 8)
(1999, 16) 
(2000, 3)};


\nextgroupplot[ymin=-13000, ymax=14500,ytick={-15000,-10000,-5000,0,5000,10000,15000,20000}]
\addplot [draw=none,fill=yellow!100] coordinates {
        (1998, 3426)
        (1999, 4953) 
        (2000, 1505)};
\addplot [draw=none, fill=green!100] coordinates {
        (1998, 2297)
        (1999, 4822) 
        (2000, 921)};

\nextgroupplot[ymin=40000, ymax=270000,ytick={0,50000,100000,150000,200000,250000,300000}]
\addplot [draw=none,fill=yellow!100] coordinates {
        (1998, 69939)
        (1999, 55986) 
        (2000, 51422)};
\addplot [draw=none, fill=red!100] coordinates {
        (1998, 74916)
        (1999, 77029) 
        (2000, 89716)};
\addplot [draw=none,fill=green!100] coordinates {
        (1998, 46452)
        (1999, 51274) 
        (2000, 53092)};


\end{groupplot}
\end{tikzpicture}
\end{document}

If you save this as e.g. plot.tex, compile it, and then do

\documentclass{article}
\usepackage[a4paper,margin=2cm,showframe]{geometry} %showframe options indicates margins
\usepackage{graphicx}
\begin{document}

\begin{figure}
\centering
\includegraphics{plot}
\end{figure}

\end{document}

you will get this output, where the black vertical lines indicate the margin:

enter image description here

7
  • Thanks a lot. I am still trying to interpret your results, but to answer your question about the use of seperate plots (and .pdf's) in my example (as well as the scaling issues you have raised), I think ... -> I did those because, in the real compilation, I use 17 different years (instead of 3 years: 1998-2000). This makes extremely wide plots. I then scale them down in the final compilation .pdf, so that the final document stays A4. I made seperate plots, so that I can easily include \caption's and footnotes (and centralized lay-out control) in the final document.
    – O0123
    Apr 18, 2016 at 20:31
  • @VincentVerheyen I see, so captions for each panel is a requirement. And with seventeen years it will probably be a bit too cramped. Should I just delete my answer? Not sure it does you any good. Apr 18, 2016 at 20:46
  • @VincentVerheyen Splitting it into three PDFs will still give you alignment problems I think. Apr 18, 2016 at 20:56
  • @VincentVerheyen Oh, sorry, must have missed that. But you can probably just add the nodes near coords=... to the options for \nextgroupplot. Apr 18, 2016 at 20:58
  • @VincentVerheyen Probably. I'll think about it, but I can't make any guarantees. Apr 18, 2016 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .