2

Here is my problem

I have tabular data exported from MATLAB using matlab2tikz. My initial plot is sort of a ribbon plot. Therefore, when exporting it to tikz, the tabular data for each ribbon is of the form

x   y   z   c
x1  y1  z1  c1
x2  y2  z2  c2
.
.
xn  yn  zn  cn
x1  y1+yy  z1  c1
x2  y2+yy  z2  c2
.
.
xn  yn+yy  zn  cn

As it doesn't seem to be possible to force matlab2tikz to export only half of the twin sets of coordinates (except for the y direction), I was wondering if it is possible to tell pgfplots to only plot half of the points from point 1 to half of the length of the table.

This could be achieved by a restrict y to domain ... but any other ideas would be greatly appreciated.

MWE

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}

\pgfplotstableread{
x y z c
0 0 1 0 
1 0 1 1
1 0 3 4
0 1 1 0 
1 1 1 1
1 1 3 4
}\mytable

\begin{axis}[%
width=5cm,
height=5cm
]

\addplot3[red] table {\mytable};

\end{axis}
\end{tikzpicture}%
\end{document}
BambOo
  • 8,801
  • 2
  • 20
  • 47

2 Answers2

2

You can use this answer.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
% from https://tex.stackexchange.com/a/199396/121799
% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}

\begin{document}
\begin{tikzpicture}

\pgfplotstableread{
x y z c
0 0 1 0 
1 0 1 1
1 0 3 4
0 1 1 0 
1 1 1 1
1 1 3 4
}\mytable
\pgfplotstablegetrowsof{\mytable}
\pgfmathsetmacro{\NumRows}{\pgfplotsretval}
\pgfmathtruncatemacro{\CutOff}{\NumRows/2-1}  
\begin{axis}[%
width=5cm,
height=5cm
]

\addplot3[red,select coords between index={0}{\CutOff}] table {\mytable};

\end{axis}
\end{tikzpicture}%
\end{document}

UPDATE: I compute the cutoff with pgfplots.

  • Thanks marmot, I guess I will delete this question since it's an exact duplicate. What do you think ? – BambOo Apr 16 '18 at 14:29
  • @BambOo It is up to you. I never really understood what the definition of a duplicate is. Apparently many people think that this question is not a duplicate of this question. If this is the case, I don't think your question is a duplicate. –  Apr 16 '18 at 14:33
  • After reading your edit, I will keep it since your answer is IMHO a bit nicer than the ones in the previous question. Just another small point. How would you do the same thing when using \addplot3 table {table.tsv}; instead of \pgfplotstableread{table.tsv}\mytable then \addplot3 table {\mytable};? – BambOo Apr 16 '18 at 14:36
  • @BambOo I don't know since in this case you do not know the total number of of rows. So you would be back to version 1 of my answer (I did not test this!), but I'd like to argue that this is too high a price to pay. –  Apr 16 '18 at 14:39
  • Sure, I agree with you. Actually, I was asking this, because I have multiple ribbons that are shifted in the y direction. But in the end, they have the same length so I only need to count the lines once and for all then apply the cutoff for all \addplot. Thanks a lot ! – BambOo Apr 16 '18 at 14:43
  • @BambOo You should not delete your question, even if it's a duplicate: someone else might find your question with a search engine and not the other one. But you can flag your own question as a duplicate if you believe it is. – anderstood Apr 17 '18 at 21:20
1

One can also edit the table to create new columns for individual ribbons. Note, the rows are numbered 0,1,2,... (column names don't count as a row).

\documentclass[tikz]{standalone}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\pgfplotstableread{
x y z c
0 0 1 0 
1 0 1 1
1 0 3 4
0 1 1 0 
1 1 1 1
1 1 3 4
}\mytable

\pgfplotstablecreatecol[create col/assign/.code={\let\entry=\empty
  \ifnum \pgfplotstablerow<3 \edef\entry{\thisrow{x}} \fi
  \pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
{xx}\mytable

\pgfplotstablecreatecol[create col/assign/.code={\let\entry=\empty
  \ifnum \pgfplotstablerow<3 \edef\entry{\thisrow{y}} \fi
  \pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
{yy}\mytable

\pgfplotstablecreatecol[create col/assign/.code={\let\entry=\empty
  \ifnum \pgfplotstablerow<3 \edef\entry{\thisrow{z}} \fi
  \pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
{zz}\mytable

\pgfplotstablecreatecol[create col/assign/.code={\let\entry=\empty
  \ifnum \pgfplotstablerow>2 \ifnum\pgfplotstablerow<6 \edef\entry{\thisrow{x}}\fi\fi
  \pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
{xxx}\mytable

\pgfplotstablecreatecol[create col/assign/.code={\let\entry=\empty
  \ifnum \pgfplotstablerow>2 \ifnum\pgfplotstablerow<6 \edef\entry{\thisrow{y}}\fi\fi
  \pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
{yyy}\mytable

\pgfplotstablecreatecol[create col/assign/.code={\let\entry=\empty
  \ifnum \pgfplotstablerow>2 \ifnum\pgfplotstablerow<6 \edef\entry{\thisrow{z}}\fi\fi
  \pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
{zzz}\mytable

\begin{tikzpicture}
\node {\pgfplotstabletypeset\mytable};
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[%
width=5cm,
height=5cm
]

\addplot3[red] table[x=xx,y=yy,z=zz] {\mytable};
\addplot3[green] table[x=xxx,y=yyy,z=zzz] {\mytable};

\end{axis}
\end{tikzpicture}%
\end{document}

demo1 demo2

John Kormylo
  • 79,712
  • 3
  • 50
  • 120