1

I have shapes (some small graphics) defined with \newcommand. These are equipped with automatically printed y-coordinate (displayed as an "altitude"). I would like to put a series of such shapes on a graph. But I have troubles with correct positioning of them in the axis coordinate system. The other problem is that the settings of the first graphics are lost after second graphics is placed (it happens only within axis environment). Code:

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

forrest
  • 958

1 Answers1

2

For some reason (I'm not able to explain why), you can not use pic in axis environment of pgfplots. A quick fix that will remove the warning message can be

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

but you still can not name the pic, so that you can not refer the node inside pic after.

There is other way without pic, if just want weird aguments, here is an example.

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

enter image description here

ZhiyuanLck
  • 4,516
  • Still one graphics is not displayed (this one, which has coordinates (55,9) in the example above (it is intentionally placed beyond the plot area). And the name "Emma" is forgotten - try to remove % in the line with \draw (Emma-head) -- +(0cm,-1cm);, please. – forrest Jul 20 '20 at 04:10
  • As you commented in code, it is beyond plot area, the effect is something like clip. You can set ymax=10 to extend "clip range" – ZhiyuanLck Jul 20 '20 at 04:24
  • I have a stack o plots, which overlap with the markers made with the use of \newcommand. Here is explained, what I do: https://tex.stackexchange.com/questions/46422/axis-break-in-pgfplots. I ktow that the plot is clipped to be bounded by the plot frame. The extention of y-axis is not a solution - the range of coordinates has to be unchanged. Moreover, the macro generated with \newcommand has to get the y-coordinete of its location and display it. Only for simplicity I used the symbol of a bird. What about the line %\draw (Emma-head) -- +(0cm,-1cm); % <- gives an error? – forrest Jul 20 '20 at 05:37
  • 1
    axis environment is actually a wrapped tikzpicture environment, you can not access the named shape that are defined inside axis from the outside. – ZhiyuanLck Jul 20 '20 at 07:14