# How to scale a matlab graphic and include it in LaTeX using matlab2tikz

I have created a Matlab graphic. Now I want to use matlab2tikz in order to include the graphic in a LaTeX document. Therefore, I scale my graphic in Matlab, using:

%LaTeX set up
height=10; width=10;
set( gcf, 'Units', 'centimeters' )
set( gcf, 'Position', [ 0 0 width height ] )
set( gcf, 'PaperUnits', 'centimeters' )
set( gcf, 'PaperSize', [ width height ] )
set( gcf, 'PaperPositionMode', 'Manual' )
set( gcf, 'PaperPosition', [ 0 0 width height ] )
box off


I then use cleanfigure() and matlab2tikz(). I save the file in my LaTeX folder and I include it in LaTeX using \input{}.

Two problems occur:

1. Due to scaling the graphic, the blue line goes a little below the x-axis.
2. The legend entry of the blue line is missing, although in matlab, it has been there!

This question is closely related to this question. In the answer from gernot, you can see the .tikz file and what the graphic looks like.

Thanks for help!

In this figure, you can see how the blue line goes below the x-axis:

this is the corresponding .tikz file:

\definecolor{mycolor1}{rgb}{1.00000,0.00000,1.00000}%
%
\begin{tikzpicture}

\begin{axis}[%
width=3.038in,
height=3.15in,
at={(0.526in,0.492in)},
scale only axis,
xmin=-30,
xmax=5,
xlabel style={font=\color{white!15!black}},
xlabel={$\text{P}_{\text{in}}\text{ [dBm]}$},
every outer y axis line/.append style={mycolor1},
every y tick label/.append style={font=\color{mycolor1}},
every y tick/.append style={mycolor1},
ymin=36,
ymax=52,
ylabel style={font=\color{mycolor1}},
ylabel={$\text{P}_{\text{out}}\text{ [dBm]}$},
axis background/.style={fill=white},
axis x line*=bottom,
axis y line*=left,
xmajorgrids,
xminorgrids,
ymajorgrids,
yminorgrids,
legend style={at={(0.03,0.97)}, anchor=north west, legend cell align=left, align=left, draw=white!15!black}
]
\addplot [color=mycolor1]
table[row sep=crcr]{%
-25.21  36.83\\
-20.22  41.62\\
-18.21  43.51\\
-17.21  44.44\\
-16.21  45.36\\
-15.25  46.21\\
-14.26  47.05\\
-13.27  47.86\\
-12.26  48.62\\
-11.76  48.98\\
-11.26  49.33\\
-10.27  49.94\\
-9.77   50.22\\
-9.27   50.47\\
-8.77   50.71\\
-7.76   51.1\\
-7.27   51.25\\
-6.76   51.38\\
-6.26   51.47\\
-5.76   51.53\\
-5.26   51.55\\
-4.76   51.53\\
-4.27   51.46\\
-3.77   51.36\\
-3.28   51.2\\
-2.77   51\\
-2.28   50.75\\
-1.78   50.46\\
-1.27   50.1\\
-0.780000000000001  49.71\\
-0.280000000000001  49.27\\
0.219999999999999   48.79\\
0.719999999999999   48.25\\
};
\addlegendentry{AM/AM}
\addlegendimage{blue}
\addlegendentry{AM/PM}
\end{axis}

\begin{axis}[%
width=3.052in,
height=3.209in,
at={(0.512in,0.433in)},
scale only axis,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
every x tick/.append style={black},
xmin=-30,
xmax=5,
xtick={-30,-25,-20,-15,-10,-5,0,5},
xticklabels={\empty},
every outer y axis line/.append style={blue},
every y tick label/.append style={font=\color{blue}},
every y tick/.append style={blue},
ymin=0,
ymax=40,
ylabel style={font=\color{blue}},
ylabel={$\text{Phi}_{\text{out}}\text{ [deg]}$},
axis x line*=top,
axis y line*=right
]
\addplot [color=blue, forget plot]
table[row sep=crcr]{%
-25.21  0\\
-24.23  0.119999999999997\\
-23.24  0.170000000000002\\
-22.23  0.25\\
-21.22  0.310000000000002\\
-20.22  0.439999999999998\\
-19.2   0.619999999999997\\
-18.21  0.93\\
-17.21  1.35\\
-16.21  1.97\\
-15.25  2.69\\
-14.26  3.7\\
-13.27  4.97\\
-12.26  6.5\\
-11.76  7.37\\
-10.76  9.26\\
-9.27   12.34\\
-8.77   13.42\\
-8.26   14.59\\
-6.76   18.15\\
-6.26   19.51\\
-4.76   24.05\\
-4.27   25.62\\
-3.28   28.66\\
-2.28   31.5\\
-1.78   32.81\\
-0.780000000000001  35.35\\
-0.280000000000001  36.64\\
0.219999999999999   37.86\\
0.719999999999999   39.02\\
};

\end{axis}
\end{tikzpicture}%

• I don't understand what you mean by point 1., the blue plot does not go below the x-axis. – Torbjørn T. Feb 12 '17 at 16:46
• Also, if the legend is present in Matlab, but missing in the pgfplots version, then it sounds as if matlab2tikz doesn't handle the way you created the legend in Matlab. I'd consider posting an issue at github.com/matlab2tikz/matlab2tikz/issues. It's easy to add the legend if you're willing to edit the .tikz file, but I think it's hard or impossible to say why it's missing in the first place, when we don't know what you did in Matlab, and it might be matlab2tikzs fault anyway. – Torbjørn T. Feb 12 '17 at 16:51
• Can you add the code that generates that image as well? The code in gernot's answer does not show that behaviour (as you can see from the image in my answer). – Torbjørn T. Feb 12 '17 at 17:20

## 1 Answer

For point 1, the size and position of the two axes are different (look at the first three options of each axis environment), so naturally they're not aligned properly. If you set the same values for both cases, e.g.

width=3.038in,
height=3.15in,
at={(0.526in,0.492in)},


it comes out as desired.

To partially answer point 2, you can get a legend for both plots by adding the following to the end of the first axis environment, i.e. after the \addlegendentry that's already there.

\addlegendimage{blue}
\addlegendentry{Whatevs}


In this case blue is the style of the second plot. If there were other options in that \addplot that related to the style of the line, you'd need to add those as well in the \addlegendimage command.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage{amsmath}
% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
%  http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{1.00000,0.00000,1.00000}%
%
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
width=3.038in,
height=3.15in,
at={(0.526in,0.492in)},
scale only axis,
xmin=-30,
xmax=5,
xlabel style={font=\color{white!15!black}},
xlabel={$\text{P}_{\text{in}}\text{ [dBm]}$},
every outer y axis line/.append style={mycolor1},
every y tick label/.append style={font=\color{mycolor1}},
every y tick/.append style={mycolor1},
ymin=36,
ymax=52,
ylabel style={font=\color{mycolor1}},
ylabel={$\text{P}_{\text{out}}\text{ [dBm]}$},
axis background/.style={fill=white},
axis x line*=bottom,
axis y line*=left,
xmajorgrids,
xminorgrids,
ymajorgrids,
yminorgrids,
legend style={at={(0.03,0.97)}, anchor=north west, legend cell align=left, align=left, draw=white!15!black}
]
\addplot [color=mycolor1]
table[row sep=crcr]{%
-25.21  36.83\\
-20.22  41.62\\
-18.21  43.51\\
-17.21  44.44\\
-16.21  45.36\\
-15.25  46.21\\
-14.26  47.05\\
-13.27  47.86\\
-12.26  48.62\\
-11.76  48.98\\
-11.26  49.33\\
-10.27  49.94\\
-9.77   50.22\\
-9.27   50.47\\
-8.77   50.71\\
-7.76   51.1\\
-7.27   51.25\\
-6.76   51.38\\
-6.26   51.47\\
-5.76   51.53\\
-5.26   51.55\\
-4.76   51.53\\
-4.27   51.46\\
-3.77   51.36\\
-3.28   51.2\\
-2.77   51\\
-2.28   50.75\\
-1.78   50.46\\
-1.27   50.1\\
-0.780000000000001  49.71\\
-0.280000000000001  49.27\\
0.219999999999999   48.79\\
0.719999999999999   48.25\\
};
\addlegendentry{AM/AM}
\addlegendimage{blue}
\addlegendentry{Whatevs}
\end{axis}

\begin{axis}[%
width=3.038in,
height=3.15in,
at={(0.526in,0.492in)},
scale only axis,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
every x tick/.append style={black},
xmin=-30,
xmax=5,
xtick={-30,-25,-20,-15,-10,-5,0,5},
xticklabels={\empty},
every outer y axis line/.append style={blue},
every y tick label/.append style={font=\color{blue}},
every y tick/.append style={blue},
ymin=0,
ymax=40,
ylabel style={font=\color{blue}},
ylabel={$\text{Phi}_{\text{out}}\text{ [deg]}$},
axis x line*=top,
axis y line*=right
]
\addplot [color=blue, forget plot]
table[row sep=crcr]{%
-25.21  0\\
-24.23  0.119999999999997\\
-23.24  0.170000000000002\\
-22.23  0.25\\
-21.22  0.310000000000002\\
-20.22  0.439999999999998\\
-19.2   0.619999999999997\\
-18.21  0.93\\
-17.21  1.35\\
-16.21  1.97\\
-15.25  2.69\\
-14.26  3.7\\
-13.27  4.97\\
-12.26  6.5\\
-11.76  7.37\\
-10.76  9.26\\
-9.27   12.34\\
-8.77   13.42\\
-8.26   14.59\\
-6.76   18.15\\
-6.26   19.51\\
-5.26   22.54\\
-4.76   24.05\\
-4.27   25.62\\
-3.28   28.66\\
-2.77   30.13\\
-2.28   31.5\\
-1.78   32.81\\
-1.27   34.09\\
-0.280000000000001  36.64\\
0.219999999999999   37.86\\
0.719999999999999   39.02\\
};
\end{axis}
\end{tikzpicture}%
\end{document}

• thx so much! - For the first part of my question, I have edited the quesiton. Pls have a look, it's almost solved.. – user123551 Feb 12 '17 at 17:18
• @Luk I've updated. With different sized axes, it's not really surprising that you got a bad result. – Torbjørn T. Feb 12 '17 at 17:48
• thanks! .. it was surprising to me, but I'm new to LaTeX. Anyways, just to know it: when I want to change the size of my figure, now that I have it as a .tikz file, is it sufficient to just change the height/width parameters, or do I have to change other parameters as well? – user123551 Feb 12 '17 at 18:07
• @Luk Well, LaTeX or not, when you have two rectangles with different sizes, you can't really make all the edges line up at the same time :) The width/height parameters are what define the size of the axis (not including the labels, because you havescale only axis), so you don't have to change anything else. (Unless you reduce the size so much that everything becomes very cramped, and that makes you want to e.g. change font size.) – Torbjørn T. Feb 12 '17 at 18:16
• ok. When I change the height tho, I get the edited figure. I did not change the at{} line, because I am not certain what it does. I figured it just determindes the mid point, maybe. – user123551 Feb 12 '17 at 18:34