This has nothing to do with polar coordinates but with the fact that the origins of the axis
and the tikzpicture
do not coincide. That is, you add points with nonzero horizontal coordinates since calc really measures the dimensions that specify the location of the coordinate relative to the origin of the tikzpicture
. So you want to use (axis direction cs:0,10)
instead of (0,10)
.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
% \usepgfplotslibrary{}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2,xmax=2,
ymin=-3,ymax=3,
% unit vector ratio=1 1;
unit vector ratio=1 1,
]
\addplot[name path=A] {2};
\addplot[name path=B] {-2};
\draw (0,0) circle (1);
\pgfplotsinvokeforeach{0,1,...,4} {
\coordinate (p#1) at (45*#1:1);
\node[fill,inner sep=1pt,circle] at (p#1) {};
\draw[name path=C] ($(p#1)+(axis direction cs:0,-10)$)
--($(p#1)+(axis direction cs:0,10)$);
%I think it should be a vertical line through point P;
% My purpose is to obtain the intersection point of the above vertical line and y=2/-2, and connect them
}
\end{axis}
\end{tikzpicture}
\end{document}
As for the statements in the comments in the code, you can use this to find intersections, but you can also compute the intersection points analytically.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
% \usepgfplotslibrary{}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2,xmax=2,
ymin=-3,ymax=3,
% unit vector ratio=1 1;
unit vector ratio=1 1,
]
\addplot[name path=A] {2};
\addplot[name path=B] {-2};
\draw (0,0) circle[radius=1];
\pgfplotsinvokeforeach{0,1,...,4} {
\coordinate (p#1) at (45*#1:1);
\node[fill,inner sep=1pt,circle] at (p#1) {};
\path[name path=C] ($(p#1)+(axis direction cs:0,-10)$)
--($(p#1)+(axis direction cs:0,10)$);
%I think it should be a vertical line through point P;
% My purpose is to obtain the intersection point of the above vertical line and y=2/-2, and connect them
\draw[name intersections={of=C and A,by=a#1},
name intersections={of=C and B,by=b#1}]
(a#1) -- (b#1);
}
\draw[red,dashed] let \p1=($(1,0)-(0,0)$) in
foreach \X in {0,1,...,4}
{({\x1*cos(\X*45)},2) -- ({\x1*cos(\X*45)},-2)};
\end{axis}
\end{tikzpicture}
\end{document}