如何将图形放在图表上?

如何将图形放在图表上?

我有一些用 定义的形状(一些小图形)\newcommand。它们配备了自动打印的 y 坐标(显示为“高度”)。我想在图表上放置一系列这样的形状。但我在轴坐标系中正确定位它们时遇到了麻烦。另一个问题是,在放置第二个图形后,第一个图形的设置会丢失(这仅在axis环境中发生)。代码:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot
  table[row sep=crcr]{%
40  8\\
45  7\\
50  6\\
55  4\\
60  4\\
};
\bird[name=Emma]{55}{9} %  <- beyond plot area (it has to be displayed)
\bird[name=Alexandra,wingspan=6mm]{50}{6}
\end{axis}

%\draw (Emma-head) -- +(0cm,-1cm); %    <- gives an error
\end{tikzpicture}
\end{document}

答案1

由于某种原因(我无法解释原因),您无法picaxis环境中使用pgfplots。可以快速修复以删除警告消息的方法是

\matrix {\pic {pictype};\\};

但你仍然不能命名图片,这样你就不能再引用图片里的节点了。

还有其他方法pic,如果只是想要奇怪的参数,这里有一个例子。

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}

\pgfkeys{
    /seagull settings/.is family,
    /seagull settings,
    name/.estore in=\seagullname,
    wingspan/.estore in=\seagullwingspan,
    default/.style={%
        name=Emma,
        wingspan=3mm
    }
}
\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)$) {altitude: #3};
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ymax=10,
]
\addplot
  table[row sep=crcr]{%
40  8\\
45  7\\
50  6\\
55  4\\
60  4\\
};
\bird[name=Emma]{55}{9} %  <- beyond plot area (it has to be displayed)
\bird[name=Alexandra,wingspan=6mm]{50}{6}
\end{axis}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容