1

Hello i'm a bit desperate.

As nobody seemed to care about my last post i tried a lot on my problem but i still couldn't find the solution.

I want to use matlab2tikz to include my matlab figures into my latex document.

Since matlab2tikz is highly recommend by this community and many others i think the error must be in my code.

I simplified my matlab code a lot but i still wasn't able to find the error.

my matlab code for the figure:

example_data1 = linspace(0,100,5);
x1 = 1:length(example_data1);
y1 = example_data1;
x2 = 1:length(example_data1);
y2 = 2 * example_data1;
x3 = 1:length(example_data1);
y3 = 3 * example_data1;

Farbe1 = [0,1,0]*0.6; % darkgreen
Farbe2 = [1,0,0]*0.8; % darkred
Farbe3 = get(groot,'DefaultAxesColorOrder') % default values
Farbe3 = Farbe3(1,:);                       % default blue 
figure(1)
    % 3 axes 

    clf 
        %------------------------------------------------------------------
        %-------------------------- plot1:      ---------------------------
        %------------------------------------------------------------------
        a = plot(x1,y1,'green','LineWidth',2,'Color',Farbe1,...
                'DisplayName','green plot')
        ax1          = gca;
        ax1.XLim     = [0    length(x1)]
        ax1.YLim     = [0 max(y1)*1.5]
        ax1.YTick    = [0:25:300]
        ax1.FontSize = 12;


        %----------------------------------------------------------------
        %-------------------------- plot2:     --------------------------
        %----------------------------------------------------------------
        ax2               = axes('Position',ax1.Position);
        b = plot(x2,y2,'blue','LineWidth',2,'Color',Farbe3,...
                'DisplayName','blue plot')    
        ax2.Color         = 'none';
        ax2.XTick         = [];
        ax2.XLim          = [0 length(x3)];
        ax2.YAxisLocation = 'right';
        ax2.FontSize      = 12;


        %----------------------------------------------------------------
        %-------------------------- plot3:      -------------------------
        %----------------------------------------------------------------
        ax3               = axes('Position',ax1.Position);
        c = plot(x3,y3,'LineWidth',2,'Color',Farbe2,...
                'DisplayName','red plot')
        ax3.XTick         = [];
        ax3.YTick         = [];
        ax3.XLim          = ax2.XLim;
        ax3.YLim          = ax2.YLim;
        ax3.Color         = 'none';
        ax3.XAxisLocation = 'top';
        ax3.YAxisLocation = 'right';
        legend(ax3,[a,b,c])

% Run matlab2tikz
matlab2tikz('myfile.tex');

i need the three axes-objects to show more complicated data on two y-axes and also 2 x-axes.

The matlab code than leeds to this figure: enter image description here

myfile.tex then looks like this for me:

% 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}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}

\begin{axis}[%
    width=4.521in,
    height=3.566in,
    at={(0.758in,0.481in)},
    scale only axis,
    xmin=0,
    xmax=5,
    ymin=0,
    ymax=150,
    ytick={  0,  25,  50,  75, 100, 125, 150, 175, 200, 225, 250, 275, 300},
    axis background/.style={fill=white}
]
\addplot [color=black!40!green, line width=2.0pt, forget plot]
  table[row sep=crcr]{%
1   0\\
2   25\\
3   50\\
4   75\\
5   100\\
};
\end{axis}

\begin{axis}[%
    width=4.521in,
    height=3.566in,
    at={(0.758in,0.481in)},
    scale only axis,
    xmin=0,
    xmax=5,
    xtick={\empty},
    ymin=0,
    ymax=200,
    yticklabel pos=right
]
\addplot [color=mycolor1, line width=2.0pt, forget plot]
  table[row sep=crcr]{%
1   0\\
2   50\\
3   100\\
4   150\\
5   200\\
};
\end{axis}

\begin{axis}[%
    width=4.521in,
    height=3.566in,
    at={(0.758in,0.481in)},
    scale only axis,
    xmin=0,
    xmax=5,
    xtick={\empty},
    ymin=0,
    ymax=200,
    ytick={\empty},
    xticklabel pos=right,
    yticklabel pos=right,
    legend style={legend cell align=left, align=left, draw=white!15!black, fill=white!94!black}
]
\addplot [color=black!20!red, line width=2.0pt]
  table[row sep=crcr]{%
1   0\\
2   75\\
3   150\\
4   225\\
5   300\\
};
\addlegendentry{red plot}

\end{axis}
\end{tikzpicture}%

I'm calling this file in a document:

\documentclass{article}
\usepackage{amsmath}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}


\begin{document}

    \input{myfile.tex}

\end{document}

Compiling this document leads to this pdf with incomplete legend. enter image description here I've looked in the following questions for help but i think they don't fit to my problem. overlap, legend capture, legend location,

Nobody before me seemed to have this problem even though it is very common to put a legend for three plots so i think there must be problem in my codes somewhere. But i can't think of any way to make it more simple cause i need these commands late for my figures.

Is matlab2tikz meant to be used different?

I also tried to solve the problem manually by editing the tikz-file. I noticed that only the last axis-enviroment has the \addlegendentry{red plot}. So i tried to put in the other axis-enviroments too. I also added the legend style={legend cell align=left, align=left, draw=white!15!black, fill=white!94!black} to theses axis-enviroments cause i want the legends to be in the same style. But this cause a lot of syntax errors unfortunately. I gave up on this path cause a solution like this is a bit unsatisfying cause i want to use similar matlab-code on many plots and that would means i'd have to change it every time.

I hope somebody can help me this time. I don't know why last time nobody wanted to help. Maybe my question was unclear or the code was too complicated. I hope this time the question is more clear.

Any help on this would be appreciated.

2
  • I would say that this is a matlab2tikz problem and not a LaTeX problem since the TeX code works as expected. Maybe rather ask the maintainer of matlab2tikz etc. Oct 4, 2017 at 22:51
  • It seems that the code puts three axis on top of each other. Manually you would have one axis and multiple addplot commands. Oct 4, 2017 at 22:53

1 Answer 1

1

If you use the following matlab code, everything works perfectly compared to your MWE. Because there I used a hold on call. However, as you mentioned in your question that you need to plot several sets of data in the same figure but with different axes you cannot use this solution unless you scale your data to have the same plotting amplitudes for each data set.

As noted by Dr.Manuel Kuehner, this is not LaTeX related but matlab/matlab2tikz related. I'd add that it may not be relevant to try to plot too much data in the same figure, you may just be fine by using several subplots (axis in tikz) next to or on top of one another. With the right positionning, it might just do the trick.

example_data1 = linspace(0,100,5);
x1 = 1:length(example_data1);
y1 = example_data1;
x2 = 1:length(example_data1);
y2 = 2 * example_data1;
x3 = 1:length(example_data1);
y3 = 3 * example_data1;

Farbe1 = [0,1,0]*0.6; % darkgreen
Farbe2 = [1,0,0]*0.8; % darkred
Farbe3 = get(groot,'DefaultAxesColorOrder') % default values
Farbe3 = Farbe3(1,:);                       % default blue 
figure(1)
hold on 
    %-------------------------- plot1:      ---------------------------
    a = plot(x1,y1,'green','LineWidth',2,'Color',Farbe1,'DisplayName','green plot');
    %-------------------------- plot2:     --------------------------
    b = plot(x2,y2,'blue','LineWidth',2,'Color',Farbe3,'DisplayName','blue plot');
    %-------------------------- plot3:      -------------------------
    c = plot(x3,y3,'LineWidth',2,'Color',Farbe2,'DisplayName','red plot') 
    legend([a,b,c]) 
 hold off

% Run matlab2tikz
matlab2tikz('myfile.tex','Standalone',true);
1
  • thank you very much for your effort. i opened a thread in the issue tracker on githup link
    – himynameis
    Oct 5, 2017 at 9:09

You must log in to answer this question.

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