matlab2tikz icon indicating copy to clipboard operation
matlab2tikz copied to clipboard

Area plots disturbs next plots

Open tomaszGorecki opened this issue 9 years ago • 7 comments

HI, I am having problems wi the following code

%minimal example for matlab2tikz

figure()
pPlot = [0; 1];
sPlot = [1; 0];
area(pPlot, sPlot,'FaceColor',[0.8 0.9 0.9],'FaceAlpha',0.5)
hold on
plot([0; 1], [0.5; 0.5],'k','LineWidth',2);


% cleanfigure;
print('pVsS','-dpdf')
matlab2tikz('Generated_Figures/pVsS.tex', 'standalone',true)

which after compilation o the tex gives: pVsS.pdf instead of pVsS.pdf

It seems that the second plot gets its axis shifted and even tilted by the first area plot. I am using Matlab 2016a on Mac. Any workarounds/fixes possible ?

tomaszGorecki avatar Jan 11 '17 16:01 tomaszGorecki

Thank you for this complete bug report.

At the moment, I have an idea of what could be going on, but not really why this is happening: probably for the area curve, we instruct pgfplots to use stacked plots such that the other line is taken skewed by the first curve.

What might work (I'm unsure, however), is trying to plot the line before the area.

This is, however, something we should investigate further (but that's going to be at soonest for this weekend).

egeerardyn avatar Jan 11 '17 20:01 egeerardyn

Hi,

Well same issue if the plot is done the other way around, but reversed.

' figure() pPlot = [0; 1]; sPlot = [1; 0]; hold on p1 = plot([0; 1], [0.5; 0.5],'k','LineWidth',2); area(pPlot, sPlot,'FaceColor',[0.8 0.9 0.9],'FaceAlpha',0.5) % uistack(p1,'top')

% cleanfigure; print('pVsS','-dpdf') matlab2tikz('Generated_Figures/pVsS.tex', 'standalone',true) ' gives:

pVsS.pdf

tomaszGorecki avatar Jan 12 '17 10:01 tomaszGorecki

I had a closer look at this problem, and I can completely confirm the behavior. It is due to the fact that we draw area plots by using the option stack plots=y in pgfplots instead of something smarter: e.g. the fillbetween library as described in the pgfplots manual.

Unfortunately, I do not have an automatic work-around except maybe for tuning the exported TikZ code by hand. In principle, those steps shouldn't be hard for your case:

  1. remove the stack plots=y option
  2. load the fillbetween library: \usepgfplotslibrary{fillbetween}
  3. add a name to your first plot in TikZ, e.g. name path=A
  4. add a path, e.g. \path[name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0); to your TikZ file
  5. add a plot command for the filling, e.g. \addplot[orange] fill between[of=A and B]; where you replace orange with the fill-related options of your original plot.

It might be necessary to add a clipping path for perfect reconstruction of your plot, which is one of the reasons why we cannot patch this up very quickly.

egeerardyn avatar Jan 15 '17 09:01 egeerardyn

The addplot[stack plots=false,... should do the trick with the line. Although, it solves the the stacking issue, the line is now behind the area (maybe we should also enforce zsort).

okomarov avatar Jan 15 '17 15:01 okomarov

Ok great the stack plots=false worked fine for me. Thanks

tomaszGorecki avatar Jan 16 '17 09:01 tomaszGorecki

z buffer = sort does not solve the line being behind the area, even though it comes after in the .tex. You need to actually move it before the .tex code of the area, not sure this is expected and maybe we should escalate to pgfplots?

@cfeuersaenger Any comments if this is expected behaviour (line behind area even though it comes after the area in the .tex)?

capture

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
    width=4.521in,
    height=3.566in,
    at={(0.758in,0.481in)},
    scale only axis,
    area style,
    stack plots=y,
    xmin=0,
    xmax=1,
    ymin=0,
    ymax=1,
    axis background/.style={fill=white}
    ]
    \addplot[fill=white!80!teal, fill opacity=0.5, draw=black, forget plot] table[row sep=crcr]{%
    0	1\\
    1	0\\
    }
    \closedcycle;
    \addplot [stack plots = false, color=black, line width=2.0pt, forget plot]
      table[row sep=crcr]{%
    0	0.5\\
    1	0.5\\
    };
    \end{axis}
\end{tikzpicture}%
\end{document}

okomarov avatar Jan 16 '17 09:01 okomarov

Thanks for this. Just want to add that it can be handled "automatically" in the matlab2tikz export on the Matlab side so manual editing isn't required, as follows.

matlab2tikz('filename',...
    'extraAxisOptions',...     
    [...
        'stack plots=false,'...
    ]...
);

Essentially, this workaround adds the axis option stack plots=false to the end, overwriting stack plots=y.

ricopicone avatar Nov 11 '17 21:11 ricopicone