Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When I use the {axis} environment, the axis are drawn automatically so I am not able to specify a name for them? I would like to be able to compute intersections with the axis as in Intersections in PGFplots. Is there a way for me to specify a name for these axis?

One solution would be for me to redraw the axis with draw=none and then specify a name for that path, but am wondering if there is a better way. Something like xaxis name path= would be ideal?

There is a every axis key, but so perhaps I could append it to that, but then how would I specify a different name for the x-axis and y-axis?

Update The intent of my question was to find intersections with the lines x=0 and y=0 (not the boxed borders that get drawn automatically). Here is a MWE attempt:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\pgfkeys{/pgfplots/Linear Axis Style/.style={
        clip=true,
        minor tick num=0,
        axis y line=center,
        axis x line=middle, 
        axis on top=true,
        x axis line style={name path=XAxisLine},
        y axis line style={name path=YAxisLine}
    }
}

\begin{document}

\newcommand*{\ShowIntersection}[2]{
\fill 
    [name intersections={of=#1 and #2, name=i, total=\t}] 
    [red, opacity=1, every node/.style={black, opacity=1}] 
    \foreach \s in {1,...,\t}{(i-\s) circle (2pt)
        node [above right] {\s}};
}

\begin{tikzpicture}
\begin{axis}[Linear Axis Style,
    xmin=-1.5, xmax=1.5,
    ymin=-1, ymax=3,
    ]

\addplot[name path global=a, mark=none, domain=-2.5:2.5, thick]%
    ({x},{x*x-0.5});%

%\ShowIntersection{a}{XAxisLine}% Do not work
%\ShowIntersection{a}{YAxisLine}

\end{axis} 
\end{tikzpicture}
\end{document}
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The simplest thing to do is to use the axis option x axis line style={name path=<name>} (you don't need to use name path global in this case, as the axes are drawn in the same scope as all other graphical objects):

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\begin{document}

\newcommand*{\ShowIntersection}[2]{
\fill 
    [name intersections={of=#1 and #2, name=i, total=\t}] 
    [red, opacity=1, every node/.style={black, opacity=1}] 
    \foreach \s in {1,...,\t}{(i-\s) circle (2pt)
        node [above right] {\s}};
}

\begin{tikzpicture}
\begin{axis}[xmin=-1.5, xmax=1.5,
    ymin=1, ymax=3,
    x axis line style={name path=AxisLine}]

\addplot[name path global=a, mark=none, domain=-2.5:2.5, thick]%
    ({x},{x*x});%
\ShowIntersection{a}{AxisLine}

\end{axis} 
\end{tikzpicture}
\end{document}

x axis line style=name path

This is not quite ideal, however: This finds the intersections with all borders, not just the x axis. This is because by default, the border is drawn using just one path. Note also that if you would use axis line style instead of x axis line style, the intersections would be found twice because axis line style applies its options to x axis line style and y axis line style, which are then in turn both applied to the border path, so you end up with two name path global options.

To fix the problem, one might try to use the option separate axis lines. In this case, x axis line style={name path=<name>} has to be changed to x axis line style={every path/.style={name path=<name>}} because the options are passed to a scope instead of a path.

Unfortunately, in our example no intersections would be found. This is because with separate axis lines, the bottom and top axis lines are drawn in that order, using two different paths but the same options. Only the top axis (or the right axis in case of y axes) ends up being assigned the name.

One way around this would be to use the options

axis lines=left,
x axis line style={every path/.style={name path=AxisLine}}

This will only draw the left and bottom axis, leading to

axis lines=left

This might be good enough for your application. If you do require a boxed plot and the ability to find the intersections with only the bottom x axis, you will have to patch the code in the file pgfplots.code.tex.

share|improve this answer
    
Sorry, I was not clear in my question: I meant intersections with the lines x=0 and y=0, not the borders. Have provided a MWE adapted from your example. –  Peter Grill Jun 29 '11 at 20:04
    
@Peter: This is because you added the new option axis on top, which means that the axis will be drawn at the very end, after you've called \ShowIntersections. If you move \ShowIntersections outside the axis environment and use name path global instead of name path, it finds the intersections but for some weird reason draws the points with an offset. If you remove axis on top, everything works, but for some reason axis y line=middle leads to intersections with the y axis being found twice. I'll have to look into why that is. –  Jake Jun 29 '11 at 23:51
    
I am trying to manually draw the lines and need access to the values of xmin, xmax, etc. Have posted a follow up question on how to access xmin xmax, ymin, ymax from within PGFplots axis environment. –  Peter Grill Jun 30 '11 at 23:12
    
@Peter Grill: I've posted an answer to that, but the problem with the intersection being found twice still appears. I investigated a bit more, and it turns out that this problem is caused by the fact that one of the points of the plot lies exactly on the y axis. If you set the domain to something slightly asymmetric, everything works fine. –  Jake Jul 1 '11 at 1:47
    
Yep, I see the same behavior.This seems like a bug then? Adding a slight asymmetry to the xaxis works, but there does appear to be a minimum asymmetry required, below which it still shows as two points. –  Peter Grill Jul 1 '11 at 2:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.