0

I am making plots with y-axis breaks by placing one \nextgroupplot above the second one. These plots have not only curves but also some additional graphics, which are assigned to bottom coordinate system but accidentally are placed above the bottom plot box. Moreover, these additional graphics have labels with y-values, as illustrated here. The problem is that:

  1. If macros producing the subsequent graphics are placed inside the groupplot environment then the graphics placed beyond the plot bounding box are invisible.
  2. On the other side, if inside the groupplot environment are defined only coordinates and after ending of this environment are executed macros for making graphics, then labels are filled not with y-values, but with vertical positions on the picture.

Firstly I am failed with application of table indexing. Now I am trying to read x and y coordinates from the table and assign to the following pairs (x,y) \coordinates inside the groupplot environment. How to correct my code, which gives error(s)?:

\documentclass[tikz.convert]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}

% https://tex.stackexchange.com/questions/56353/extract-x-y-coordinate-of-an-arbitrary-point-on-curve-in-tikz
\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother

\pgfkeys{
    /seagull settings/.is family,
    /seagull settings,
    name/.estore in=\seagullname,
    wingspan/.estore in=\seagullwingspan,
    default/.style={%
        name=Emma,
        wingspan=3mm
    }
}
\tikzset{
    pics/seagull/.style args={xy #1:#2 span #3}{
        foreground code={
            \draw ($(#1,#2)-(#3,0mm)$) coordinate (-left wing)
                .. controls +(1mm,1mm) and +(-1mm,1mm) .. +($1*(#3,0mm)$) coordinate (-head)
                .. controls +(1mm,1mm) and +(-1mm,1mm) .. +($2*(#3,0mm)$) coordinate (-right wing);
        }
    }
}
\newcommand\bird[3][]{
    \pgfkeys{/seagull settings,default,#1}
    \path pic (\seagullname) {seagull=xy {#2}:{#3} span {\seagullwingspan}};
    \node at ($(\seagullname-head)+(0mm,3mm)$) {altitude: #3};
}

% Table with birds localizations and wingspans
\pgfplotstableread[row sep=crcr]{
    45  5   0.4\\
    52  7   0.2\\
    57  9   0.2\\
}\myloadedtable
% Rows amount in the table:
\pgfplotstablegetrowsof{\myloadedtable}
\pgfmathtruncatemacro{\noofrows}{\pgfplotsretval}
% Number of last row (numbering starts at 0)
\pgfmathtruncatemacro{\lastrowno}{\noofrows-1}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table[row sep=crcr]{%
    40  8\\
    45  7\\
    50  6\\
    55  4\\
    60  4\\
};
\foreach \i in {0,1,...,\lastrowno}{
    \pgfplotstablegetelem{\i}{[index]0}\of{\myloadedtable}
    \pgfmathsetmacro{\xcoord}{\pgfplotsretval}
    \pgfplotstablegetelem{\i}{[index]1}\of{\myloadedtable}
    \pgfmathsetmacro{\ycoord}{\pgfplotsretval}
    \coordinate (p-\i) at (\xcoord,\ycoord);
}
\end{axis}

\foreach \i in {0,1,...,\lastrowno}{
    \pgfplotstablegetelem{\i}{[index]2}\of{\myloadedtable}
    \pgfmathsetmacro{\birdsize}{\pgfplotsretval}
    \gettikzxy{(p-\i)}{\xpos}{\ypos}
    \bird[name=bird-\i,wingspan=\birdsize]{\xpos}{\ypos}
}
\end{tikzpicture}
\end{document}

1 Answer 1

0

Now I have found the similar question asked and answered by @Chris. He pointed two topics with important answers: \foreach not behaving in axis environment and Use macro to draw inside pgfplot/axis.

Now the code is:

\documentclass[tikz.convert]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}

% https://tex.stackexchange.com/questions/56353/extract-x-y-coordinate-of-an-arbitrary-point-on-curve-in-tikz
\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother

\pgfkeys{
    /seagull settings/.is family,
    /seagull settings,
    name/.estore in=\seagullname,
    wingspan/.estore in=\seagullwingspan,
    yposinaxiscs/.estore in=\currentceiling,
    default/.style={%
        name=Emma,
        wingspan=3mm,
        yposinaxiscs=0,
    }
}
\tikzset{
  seagull/.code args={xy #1:#2 span #3}{
    \edef\seagullcode##1{%
      \noexpand\draw ($(#1,#2)-(#3,0mm)$) coordinate (##1-left wing)
        .. controls +(1mm,1mm) and +(-1mm,1mm) .. +($1*(#3,0mm)$) coordinate (##1-head)
        .. controls +(1mm,1mm) and +(-1mm,1mm) .. +($2*(#3,0mm)$) coordinate (##1-right wing);
    }
  }
}
\newcommand\bird[3][]{
    \pgfkeys{/seagull settings,default,#1}
    \tikzset{seagull=xy {#2}:{#3} span {\seagullwingspan}}
    \seagullcode{\seagullname}
    \node at ($(\seagullname-head)+(0mm,3mm)$) {ceiling: \currentceiling};
}

\pgfplotstableread[row sep=crcr]{
    45  5   0.4\\
    52  7   0.4\\
    57  9   0.2\\
}\myloadedtable
% Rows amount in the table:
\pgfplotstablegetrowsof{\myloadedtable}
\pgfmathtruncatemacro{\noofrows}{\pgfplotsretval}
% Number of last row (numbering starts at 0)
\pgfmathtruncatemacro{\lastrowno}{\noofrows-1}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table[row sep=crcr]{%
    40  8\\
    45  7\\
    50  6\\
    55  4\\
    60  4\\
};
\pgfplotsinvokeforeach {0,1,...,\lastrowno}{
    \pgfplotstablegetelem{#1}{[index]0}\of{\myloadedtable}
    \pgfmathsetmacro{\xcoord}{\pgfplotsretval}
    \pgfplotstablegetelem{#1}{[index]1}\of{\myloadedtable}
    \pgfmathsetmacro{\ycoord}{\pgfplotsretval}
    % https://tex.stackexchange.com/questions/170664/foreach-not-behaving-in-axis-environment
    % https://tex.stackexchange.com/questions/73390/use-macro-to-draw-inside-pgfplot-axis
    \edef\tmp{\noexpand\coordinate (p-#1) at (\xcoord,\ycoord);}
    \tmp
}
\end{axis}

\foreach \i in {0,1,...,\lastrowno}{
    \pgfplotstablegetelem{\i}{[index]1}\of{\myloadedtable}
    \pgfmathsetmacro{\ceiling}{\pgfplotsretval}
    \pgfplotstablegetelem{\i}{[index]2}\of{\myloadedtable}
    \pgfmathsetmacro{\birdsize}{\pgfplotsretval}
    \gettikzxy{(p-\i)}{\xpos}{\ypos}
    \bird[name=bird-\i,wingspan=\birdsize,yposinaxiscs=\ceiling]{\xpos}{\ypos}
}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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