Consider the following code where I detect the crossing of a function to a given threshold.
I am plotting the line from the intersection to the x-axis, and storing the intersection in \p1
I would like to add a second axis environment identical to the first one with only an additional tick at \x1
\documentclass{article}
\usepackage{pgfplots,tikz}
\usetikzlibrary{intersections,calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
clip=true,
axis x line=bottom,
axis y line=left,
grid = major,
grid style={dashed},
xmin=1,
xmax=64,
samples =32,
ymax = 1.6,
ymin = -0.2,
xlabel={\emph{sample number}},
xlabel style={at={(1,-0.1)}, anchor=south},
legend cell align=left,
legend pos=outer north east,
]
\addplot[name path global=one,blue,mark=+,domain=1:64] {exp(-log10(2)/(8^2/4)*(\x-32)^2)};
\addplot[name path global=two,red,domain=1:64] {0.5};
\path [name intersections={of=one and two, name=i}];
\path let \p1=($(i-1)$) in (\x1,\y1);
\draw[dashed] (i-1) -- ($(axis cs:0,-0.2)!(i-1)!(axis cs:64,-.2)$);
\legend{Sampled signal,Threshold}
\end{axis}
\begin{axis}[
clip=true,
axis x line=bottom,
axis y line=left,
xmin=1,
xmax=64,
samples =32,
ymax = 1.6,
ymin = -0.2,
xtick=\empty,
ytick=\empty,
extra x ticks={\x1},
extra x tick labels={$t_{step}$},
]
\end{axis}
\end{tikzpicture}
\end{document}
My attempt to use \x1
directly in the axis environment is failing.
\x1
is only available within the context of thelet
command, so you'll need to store it in a global macro to access it later on. However, I doubt that that will be of much use, since\x1
is in "paper units", not "data units", so you can't simply feed it toextra x tick
. Do you actually want a second axis (i.e. are you going to add plots to that axis), or do you simply want to label the x position of the intersection?\x1
. But I would like it to have the same style than the axis. I figure adding an other axis would be the simplest solution. Is there no way to cast \x1 into "data units"?exp(-log10(2)/(8^2/4)*(\x-32)^2) == 0.5
by hand? It might be not portable, but sometimes you have to lower your sights relating to automation.