4

I export several plots from matlab to tikz using matlab2tikz. Many of them use colors and can often be grouped. It seems however difficult to include a single colorbar in matlab for a group of subplots and export this using matlab2tikz. I decided to export all plots seperatly and reassemble them in TeX. This gave me additional control over the subcaptions of the plots.

I am struggling now with aligning the colorbar to the axis of the different subplots. I am aware, that this might be easier using pgfplot and exporting only data from matlab. However, this is not an option at the moment due to limited time and difficult access to the matlab files.

What i want to achieve: Align the colorbar left side with the y-axis of the first subplot. Align the colorbar right side with the right y-axis of the second subplot. In other plots, a similar problem occurs with placing the colorbar on the right side of the plots (i had the impression that this might not be quite the same kind of problem due to arranging of subfigure package.) If any of you would have advice on how to place the subcaptions centered below the y-label of the subplot, i would be pleased. You find a MWE attached, the colors from the plots are neglected and so is the data. Thanks in advance!

enter image description here

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{pgfplots}

\newlength{\plotwidth}
\newlength{\plotheight}
\newlength{\xlabelwidth}
\newlength{\ylabelwidth}

\begin{document}
    \begin{figure}[ptb]
        \begin{small}
            \begin{subfigure}[t]{1.0\textwidth}
                \centering
                \begin{tikzpicture}
                \begin{axis}[
                hide axis,
                scale only axis,
                height=0pt,
                width=0pt,
                colormap/jet,
                colorbar horizontal,
                point meta min=0,
                point meta max=30,
                colorbar style={
                    width=10cm,
                    xtick={0,5,...,30},
                    ylabel style={},
                    xticklabel pos=upper
                },
                title style={xshift=4.7cm},
                title={Symbol4}]
                \addplot [draw=none] coordinates {(0,0)};
                \end{axis}
                \end{tikzpicture}
            \end{subfigure}
            \begin{tabular}{c c}
                \setlength{\plotwidth}{0.48\textwidth}
                \setlength{\plotheight}{0.3\textwidth}      
                \setlength{\xlabelwidth}{10em}
                \setlength{\ylabelwidth}{2.5em}             
                \begin{subfigure}[t]{0.5\textwidth}
                    \vspace*{0.25cm}
                    \centering
                    \begin{tikzpicture}
                    \begin{axis}[width=\plotwidth,
                    height=\plotheight,
                    xlabel=Symbol1,
                    ylabel=Symbol2,yticklabel style={text width=\ylabelwidth,align=right},xticklabel style={text width=\xlabelwidth,align=center},tick label style={/pgf/number format/fixed}]
                    \addplot[color=red,mark=x] coordinates {
                        (2,-2.8559703)
                        (3,-3.5301677)
                        (4,-4.3050655)
                        (5,-5.1413136)
                        (6,-6.0322865)
                        (7,-6.9675052)
                        (8,-7.9377747)
                    };
                    \end{axis}
                    \end{tikzpicture}
                    \caption{Data 1}
                \end{subfigure} & 
                \setlength{\plotwidth}{0.48\textwidth}
                \setlength{\plotheight}{0.3\textwidth}
                \setlength{\xlabelwidth}{10em}
                \setlength{\ylabelwidth}{2.5em}         
                \begin{subfigure}[t]{0.5\textwidth}
                    \vspace*{0.25cm}
                    \centering
                    \begin{tikzpicture}
                    \begin{axis}[width=\plotwidth,
                    height=\plotheight,
                    xlabel=Symbol1,
                    ylabel=Symbol2,yticklabel style={text width=\ylabelwidth,align=right},xticklabel style={text width=\xlabelwidth,align=center},tick label style={/pgf/number format/fixed}]
                    \addplot[color=red,mark=x] coordinates {
                        (2,-2.8559703)
                        (3,-3.5301677)
                        (4,-4.3050655)
                        (5,-5.1413136)
                        (6,-6.0322865)
                        (7,-6.9675052)
                        (8,-7.9377747)
                    };
                    \end{axis}
                    \end{tikzpicture}
                    \caption{Data 2}
                \end{subfigure} \\  
                \setlength{\plotwidth}{0.48\textwidth}
                \setlength{\plotheight}{0.3\textwidth}  
                \setlength{\xlabelwidth}{10em}
                \setlength{\ylabelwidth}{2.5em}         
                \begin{subfigure}[t]{0.5\textwidth}
                    \vspace*{0.25cm}
                    \centering
                    \begin{tikzpicture}
                    \begin{axis}[width=\plotwidth,
                    height=\plotheight,
                    xlabel=Symbol1,
                    ylabel=Symbol2,yticklabel style={text width=\ylabelwidth,align=right},xticklabel style={text width=\xlabelwidth,align=center},tick label style={/pgf/number format/fixed}]
                    \addplot[color=red,mark=x] coordinates {
                        (2,-2.8559703)
                        (3,-3.5301677)
                        (4,-4.3050655)
                        (5,-5.1413136)
                        (6,-6.0322865)
                        (7,-6.9675052)
                        (8,-7.9377747)
                    };
                    \end{axis}
                    \end{tikzpicture}
                    \caption{Data 3}
                \end{subfigure} & 
                \setlength{\plotwidth}{0.48\textwidth}
                \setlength{\plotheight}{0.3\textwidth}
                \setlength{\xlabelwidth}{10em}
                \setlength{\ylabelwidth}{2.5em}         
                \begin{subfigure}[t]{0.5\textwidth}
                    \vspace*{0.25cm}
                    \centering
                    \begin{tikzpicture}
                    \begin{axis}[width=\plotwidth,
                    height=\plotheight,
                    xlabel=Symbol1,
                    ylabel=Symbol2,yticklabel style={text width=\ylabelwidth,align=right},xticklabel style={text width=\xlabelwidth,align=center},tick label style={/pgf/number format/fixed}]
                    \addplot[color=red,mark=x] coordinates {
                        (2,-2.8559703)
                        (3,-3.5301677)
                        (4,-4.3050655)
                        (5,-5.1413136)
                        (6,-6.0322865)
                        (7,-6.9675052)
                        (8,-7.9377747)
                    };
                    \end{axis}
                    \end{tikzpicture}
                    \caption{Data 4}
                \end{subfigure} \\
            \end{tabular}
        \end{small} 
        \caption{My data}
    \end{figure}
\end{document}
1

You could manually set the positon with at={(0.252,0.0)},anchor=north west, and the width with width=11.156cm, (try and error). Also you have to remove %height=0pt, %width=0pt,.

Result: enter image description here

MWE:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{pgfplots}

\newlength{\plotwidth}
\newlength{\plotheight}
\newlength{\xlabelwidth}
\newlength{\ylabelwidth}

\begin{document}
    \begin{figure}[ptb]
        \begin{small}
            \begin{subfigure}[t]{1.0\textwidth}
                \centering
\begin{tikzpicture}
\begin{axis}[
hide axis,
scale only axis,
%height=0pt,
%width=0pt,
colormap/jet,
colorbar horizontal,
point meta min=0,
point meta max=30,
colorbar style={
    width=11.156cm,
    xtick={0,5,...,30},
    ylabel style={},
    xticklabel pos=upper,
    at={(0.252,0.0)},anchor=north west,               
    title={Symbol4},
    title style={xshift=0cm} 
    }
 ]
    \addplot[draw=none] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}
            \end{subfigure}
            \begin{tabular}{c c}
                \setlength{\plotwidth}{0.48\textwidth}
                \setlength{\plotheight}{0.3\textwidth}      
                \setlength{\xlabelwidth}{10em}
                \setlength{\ylabelwidth}{2.5em}             
                \begin{subfigure}[t]{0.5\textwidth}
                    \vspace*{0.25cm}
                    \centering
                    \begin{tikzpicture}
                    \begin{axis}[width=\plotwidth,
                    height=\plotheight,
                    xlabel=Symbol1,
                    ylabel=Symbol2,yticklabel style={text width=\ylabelwidth,align=right},xticklabel style={text width=\xlabelwidth,align=center},tick label style={/pgf/number format/fixed}]
                    \addplot[color=red,mark=x] coordinates {
                        (2,-2.8559703)
                        (3,-3.5301677)
                        (4,-4.3050655)
                        (5,-5.1413136)
                        (6,-6.0322865)
                        (7,-6.9675052)
                        (8,-7.9377747)
                    };
                    \end{axis}
                    \end{tikzpicture}
                    \caption{Data 1}
                \end{subfigure} & 
                \setlength{\plotwidth}{0.48\textwidth}
                \setlength{\plotheight}{0.3\textwidth}
                \setlength{\xlabelwidth}{10em}
                \setlength{\ylabelwidth}{2.5em}         
                \begin{subfigure}[t]{0.5\textwidth}
                    \vspace*{0.25cm}
                    \centering
                    \begin{tikzpicture}
                    \begin{axis}[width=\plotwidth,
                    height=\plotheight,
                    xlabel=Symbol1,
                    ylabel=Symbol2,yticklabel style={text width=\ylabelwidth,align=right},xticklabel style={text width=\xlabelwidth,align=center},tick label style={/pgf/number format/fixed}]
                    \addplot[color=red,mark=x] coordinates {
                        (2,-2.8559703)
                        (3,-3.5301677)
                        (4,-4.3050655)
                        (5,-5.1413136)
                        (6,-6.0322865)
                        (7,-6.9675052)
                        (8,-7.9377747)
                    };
                    \end{axis}
                    \end{tikzpicture}
                    \caption{Data 2}
                \end{subfigure} \\  
                \setlength{\plotwidth}{0.48\textwidth}
                \setlength{\plotheight}{0.3\textwidth}  
                \setlength{\xlabelwidth}{10em}
                \setlength{\ylabelwidth}{2.5em}         
                \begin{subfigure}[t]{0.5\textwidth}
                    \vspace*{0.25cm}
                    \centering
                    \begin{tikzpicture}
                    \begin{axis}[width=\plotwidth,
                    height=\plotheight,
                    xlabel=Symbol1,
                    ylabel=Symbol2,yticklabel style={text width=\ylabelwidth,align=right},xticklabel style={text width=\xlabelwidth,align=center},tick label style={/pgf/number format/fixed}]
                    \addplot[color=red,mark=x] coordinates {
                        (2,-2.8559703)
                        (3,-3.5301677)
                        (4,-4.3050655)
                        (5,-5.1413136)
                        (6,-6.0322865)
                        (7,-6.9675052)
                        (8,-7.9377747)
                    };
                    \end{axis}
                    \end{tikzpicture}
                    \caption{Data 3}
                \end{subfigure} & 
                \setlength{\plotwidth}{0.48\textwidth}
                \setlength{\plotheight}{0.3\textwidth}
                \setlength{\xlabelwidth}{10em}
                \setlength{\ylabelwidth}{2.5em}         
                \begin{subfigure}[t]{0.5\textwidth}
                    \vspace*{0.25cm}
                    \centering
                    \begin{tikzpicture}
                    \begin{axis}[width=\plotwidth,
                    height=\plotheight,
                    xlabel=Symbol1,
                    ylabel=Symbol2,yticklabel style={text width=\ylabelwidth,align=right},xticklabel style={text width=\xlabelwidth,align=center},tick label style={/pgf/number format/fixed}]
                    \addplot[color=red,mark=x] coordinates {
                        (2,-2.8559703)
                        (3,-3.5301677)
                        (4,-4.3050655)
                        (5,-5.1413136)
                        (6,-6.0322865)
                        (7,-6.9675052)
                        (8,-7.9377747)
                    };
                    \end{axis}
                    \end{tikzpicture}
                    \caption{Data 4}
                \end{subfigure} \\
            \end{tabular}
        \end{small} 
        \caption{My data}
    \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.