1

SOLVED: How would you plot subset of a function in Tikz:

In Syntax for smoothing a coordinate plot with the foreach command

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections,calc}

\begin{document}

\begin{tikzpicture}
% red line
\path[draw,red,name path=one] plot [smooth] coordinates {(0,0) (7.5,0) (8,6) (8.5,0) (10,0)};
% function
\path[draw,blue,name path=three]
plot[domain=0:10,samples=100,smooth] (\x,{0.5*rand+2});
red line + function
\foreach \c in {0,...,100} {
  \pgfmathsetmacro{\x}{\c/10}
  \path[name path=line] (\x,-1) -- (\x,4);
  \path[name intersections={of=one and line,name=newone}];
  \path[name intersections={of=three and line,name=newthree}];
  \path let \p1=(newone-1), \p2=(newthree-1) in 
  (\x1,\y1+\y2) coordinate (sum-\c);
}
\def\pts{}
\foreach \x in {0,...,100} { \xdef\pts{\pts (sum-\x)}; }
\draw[black] plot[smooth] coordinates {\pts};
\end{tikzpicture}

\end{document}

I would like to plot only the 23 first coordinates of \pts

This is not correct but looking for something like:

\path[draw,black] plot [smooth] coordinates {\pts(sum-0),...,\pts(sum-23)};

Solution:

\def\ptsa{}
\foreach \a in {0,...,23} {\xdef\ptsa{\ptsa(sum-\a)}; }
\path[draw,green] plot [smooth] coordinates {\ptsa};

Produces:enter image description here

  • 2
    \foreach \x in {0,...,23} { \xdef\pts{\pts (sum-\x)}; } should do it. – Loop Space Apr 15 '13 at 12:58
  • 2
    I'm not sure I understand the question. If you replace \foreach \x in {0,...,100} { \xdef\pts{\pts (sum-\x)}; } by \foreach \x in {0,...,23} { \xdef\pts{\pts (sum-\x)}; }, do you not get what you want? – jub0bs Apr 15 '13 at 13:00
  • You both understood the question well. I tried redefining a new set of point as you suggest: \def\ptsa{} \foreach \a in {0,...,23} {\xdef\ptsa{\pts(sum-\a)}; }; \path[draw,black] plot [smooth] coordinates {\ptsa}; But for some reason the subplot does not follow the full original plot – vrleboss Apr 15 '13 at 13:06
  • 1
    Use \def\ptsa{} \foreach \a in {0,...,23} {\xdef\ptsa{\ptsa(sum-\a)}; };... (Don't reuse \pts!) – Paul Gaborit Apr 15 '13 at 13:50
  • 1
    Please update your question and explain what the problem is; so far, it remains unclear. Besides, did you mean \def\ptsa{} \foreach \a in {0,...,23} {\xdef\ptsa{\ptsa(sum-\a)}; } \path[draw,black] plot [smooth] coordinates {\ptsa}; instead? – jub0bs Apr 15 '13 at 13:53

Browse other questions tagged or ask your own question.