3

I was wondering if there is a way to make a rectangle box slightly above a tikzfigure, used for labelling purposes? I have demonstrated what I thought it should look like, as:

where the dark grey box with EV/EBIT is the box I want to create. Obviously for this to look nice, it should have the exact same width, as the figure. I am using the following code for the graph:

    \documentclass{article}

\usepackage{pgfplots, pgfplotstable}
\usepackage{tikz}


\begin{document}

\begin{figure}[h] 
\caption{Time series median of APE for the EV/EBIT multiple based on combinatorial selection schemes}
\label{Figure.TimeseriesofAPE}
\centering 
\begin{tikzpicture}   
\begin{axis}[width=14cm,height=8cm,
            ylabel={Absolute Percentage Error},
            xmin=2003.5,
            xmax=2018.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=40},
            ytick={15, 20, 25, 30, 35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,

]
            \addplot [semithick,mark=square*,blue,mark options={scale=1.2}] coordinates {
(2004,23.9477345892197)
(2005,24.0195465335992)
(2006,20.6276481251735)
(2007,18.265284388086)
(2008,21.8111180034732)
(2009,24.2629967323528)
(2010,22.2193898469998)
(2011,23.4662001690915)
(2012,22.3983479598189)
(2013,21.6518435739983)
(2014,20.7295962241899)
(2015,21.5151016657556)
(2016,22.728213671688)
(2017,20.6614535846647)
(2018,23.2079687708666)


    };
\addlegendentry{SBP Plus SARD}
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(2004,30.6453384436133)
(2005,25.9071572285215)
(2006,21.5368656549373)
(2007,23.1619489291921)
(2008,22.716278332465)
(2009,26.6295451295127)
(2010,25.7656563953101)
(2011,21.0579081996823)
(2012,23.9734983213042)
(2013,27.0328903793312)
(2014,23.5204904251705)
(2015,24.5540581415406)
(2016,27.393772936285)
(2017,23.77644107908)
(2018,24.9426613487672)

    };
\addlegendentry{GICS Plus SARD}    
            \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}
  • Could you expand your code to a compilable one please? – user156344 May 7 at 12:33
  • Hi Joule, fixed it with begin and end document. – Philip May 7 at 12:35
5

pgfplots has the title key for this:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{tikz}


\begin{document}

\begin{figure}[h] 
\caption{Time series median of APE for the EV/EBIT multiple based on combinatorial selection schemes}
\label{Figure.TimeseriesofAPE}
\centering 
\begin{tikzpicture}   
\begin{axis}[width=14cm,height=8cm,
            ylabel={Absolute Percentage Error},
            xmin=2003.5,
            xmax=2018.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=40},
            ytick={15, 20, 25, 30, 35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
            title=\fcolorbox{black}{gray!50!white}{\makebox[12.17cm]{EV/EBIT}},
    title style={yshift=-0.33cm},
]
            \addplot [semithick,mark=square*,blue,mark options={scale=1.2}] coordinates {
(2004,23.9477345892197)
(2005,24.0195465335992)
(2006,20.6276481251735)
(2007,18.265284388086)
(2008,21.8111180034732)
(2009,24.2629967323528)
(2010,22.2193898469998)
(2011,23.4662001690915)
(2012,22.3983479598189)
(2013,21.6518435739983)
(2014,20.7295962241899)
(2015,21.5151016657556)
(2016,22.728213671688)
(2017,20.6614535846647)
(2018,23.2079687708666)


    };
\addlegendentry{SBP Plus SARD}
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(2004,30.6453384436133)
(2005,25.9071572285215)
(2006,21.5368656549373)
(2007,23.1619489291921)
(2008,22.716278332465)
(2009,26.6295451295127)
(2010,25.7656563953101)
(2011,21.0579081996823)
(2012,23.9734983213042)
(2013,27.0328903793312)
(2014,23.5204904251705)
(2015,24.5540581415406)
(2016,27.393772936285)
(2017,23.77644107908)
(2018,24.9426613487672)

    };
\addlegendentry{GICS Plus SARD}    
            \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

  • That is great, thank you! How will I be able to add something like lines to the box, along with adjusting the distance to the figure? I'd like them to be as close as the picture I provided :) – Philip May 7 at 12:40
  • @Philip I just added lines – user188399 May 7 at 12:41
  • Aah, so that's the fcolorbox, cool! – Philip May 7 at 12:42
  • @Philip See again for the distance – user188399 May 7 at 12:44
  • is there any reason why the rgb color setting is different, compared to other elements where I've used the same RGB code? It is much lighter in the title box, compared to elsewhere – Philip May 7 at 12:59
2

It is better to draw the title box with tikz by adding a title style specification to produce:

enter image description here

Here is your modified MWE.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{figure}[h]
\caption{Time series median of APE based on combinatorial selection schemes}
\label{Figure.TimeseriesofAPE}
\centering
\begin{tikzpicture}
\begin{axis}[width=14cm,height=8cm,
            ylabel={Absolute Percentage Error},
            xmin=2003.5,
            xmax=2018.5,
            ymin=15,
            ymax=35,
            xtick=data,
            xticklabel style=
            {/pgf/number format/1000 sep=,rotate=40},
            ytick={15, 20, 25, 30, 35},
            axis background/.style={fill=gray!5},
            legend style={fill=gray!5},
            xtick pos=lower,ytick pos=left,
            yticklabel=\pgfkeys{/pgf/number format/.cd,fixed,precision=0,zerofill}\pgfmathprintnumber{\tick}\%,
            title={\textbf{EV/EBIT}},
            title style={yshift=-2.2mm,fill={rgb:red,0;green,48;blue,133},
                         text=white,rectangle, minimum width=124.5mm},
]
            \addplot [semithick,mark=square*,black,mark options={scale=1.2}] coordinates {
(2004,23.9477345892197)
(2005,24.0195465335992)
(2006,20.6276481251735)
(2007,18.265284388086)
(2008,21.8111180034732)
(2009,24.2629967323528)
(2010,22.2193898469998)
(2011,23.4662001690915)
(2012,22.3983479598189)
(2013,21.6518435739983)
(2014,20.7295962241899)
(2015,21.5151016657556)
(2016,22.728213671688)
(2017,20.6614535846647)
(2018,23.2079687708666)


    };
\addlegendentry{SBP Plus SARD}
\addplot[semithick,mark=*,red,mark options={scale=1.2}]
coordinates{
(2004,30.6453384436133)
(2005,25.9071572285215)
(2006,21.5368656549373)
(2007,23.1619489291921)
(2008,22.716278332465)
(2009,26.6295451295127)
(2010,25.7656563953101)
(2011,21.0579081996823)
(2012,23.9734983213042)
(2013,27.0328903793312)
(2014,23.5204904251705)
(2015,24.5540581415406)
(2016,27.393772936285)
(2017,23.77644107908)
(2018,24.9426613487672)

    };
\addlegendentry{GICS Plus SARD}
            \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

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.