如何在图表上放置一系列小图形?

如何在图表上放置一系列小图形?

我正在制作带有 y 轴断点的图将一个放在\nextgroupplot第二个之上。这些图不仅有曲线,还有一些附加图形,这些图形被分配到底部坐标系,但意外地放置在底部图框上方。此外,这些附加图形具有带有 y 值的标签,如图所示这里。问题是:

  1. 如果将产生后续图形的宏放置在groupplot环境内部,那么放置在绘图边界框之外的图形是不可见的。
  2. 另一方面,如果在groupplot环境内仅定义坐标,并且在此环境结束后执行制作图形的宏,则标签不是用 y 值填充,而是用图片上的垂直位置填充。

首先,我申请失败了表索引。现在我尝试从表中读取 x 和 y 坐标,并将其分配给环境\coordinate中的以下对 (x,y) groupplot。如何纠正出现错误的代码?:

\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

现在我找到了类似问题@Chris 提问并回答。他指出了两个主题并给出了重要的答案:\foreach 在轴环境中不起作用使用宏在 pgfplot/axis 内绘图

现在代码是:

\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}

在此处输入图片描述

相关内容