我似乎在轴坐标系中放置坐标没有问题,并且在 x 方向上平移似乎也按预期工作。 但是,当我尝试在 y 方向上平移时,我对结果感到困惑:
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major,]
\addplot plot coordinates{(1800,2.14) (1860,2.18)};
\coordinate (bottomLeft) at (axis cs:1800,2.14);
\coordinate (bottomRight) at ($(bottomLeft) + (30,0)$);
\draw (bottomLeft) -- (bottomRight);
\coordinate (topLeft) at ($(bottomLeft) + (0,0.02)$);
\coordinate (topRight) at ($(topLeft) + (20,0)$);
\draw[red,thick] (topLeft) -- (topRight);
\end{axis}
\end{tikzpicture}
\end{document}
生成
并显示我两次都沿 x 方向平移,但 y 方向 (0,0.02) 的平移没有按我预期的方式计算 - 我想要一个坐标 (1800,2.16)。您现在可以跳到底部,除非您想查看我未完成的故障排除。
我发现正在进行计算
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major,]
\addplot plot coordinates{(1800,2.14) (1860,2.18)};
\coordinate (bottomLeft) at (axis cs:1800,2.14);
\draw[purple] ($(bottomLeft) + (0,0.2)$) circle (0.3cm);
\filldraw[purple] ($(bottomLeft) + (0,10)$) circle (0.3cm);
\end{axis}
\end{tikzpicture}
\end{document}
因为我能够在 y 方向上平移 - 但它似乎没有像在 x 方向上那样在轴 cs 上平移:
空心圆看起来根本没有平移,但仔细检查后,我发现实心圆也平移了(距离似乎与轴 cs 无关)。我想 y 轴的处理方式可能与 x 轴不同,所以我尝试再次指定轴 cs:
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major,]
\addplot plot coordinates{(600,1) (1800,2.14) (1860,2.18)};
\coordinate (bottomLeft) at (axis cs:1800,2.14);
\coordinate (test) at ($(bottomLeft) + (axis cs:0,0.2)$);
\draw[thick,purple] (test) circle (0.3cm);
\draw[thick] (bottomLeft) -- (test);
\end{axis}
\end{tikzpicture}
\end{document}
但改变比例(通过改变左下点)表明(测试)相对于轴 cs 并不停留在同一位置:
我尝试为整个计算指定坐标轴和不同的括号位置,但都无法编译。
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major,]
\addplot plot coordinates{(800,1) (1800,2.14) (1860,2.18)};
\coordinate (bottomLeft) at (axis cs:1800,2.14);
%\coordinate (test) at (axis cs:$(bottomLeft) + (0,0.2)$);%didn't compile
%\coordinate (test) at (axis cs:{$(bottomLeft) + (0,0.2)$});%didn't compile
%\coordinate (test) at (axis cs:$(bottomLeft) + {(0,0.2)}$);%didn't compile
%\coordinate (test) at (axis cs:${(bottomLeft) + (0,0.2)}$);%didn't compile
\coordinate (translation) at (axis cs:0,0.2);
\coordinate (test) at ($(bottomLeft) + (translation)$);
\draw[thick,purple] (test) circle (0.3cm);
\draw[thick] (bottomLeft) -- (test);
\end{axis}
\end{tikzpicture}
\end{document}
当我尝试在计算之前创建两个坐标时,(测试)似乎仍然没有随轴 cs 移动:
有人知道如何使用轴坐标系中定义的另外两个坐标来计算坐标位置吗?
答案1
正如手册中所述pgfplots
:
如文档中所述
axis cs
,通过 TikZ++
运算符添加两个坐标1可能会起到意想不到的效果。正确的操作方式++
是axis direction cs
。
1尽管您在这里并没有严格使用++
,但就这一点而言,+
从库中获取的内容实际上是相同的。calc
要进行相对坐标变换,请使用axis direction cs
而不是axis cs
:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major,]
\addplot plot coordinates{(1800,2.14) (1860,2.18)};
\coordinate (bottomLeft) at (axis cs:1800,2.14);
\coordinate (bottomRight) at ($(bottomLeft) + (axis direction cs:30,0)$);
\draw (bottomLeft) -- (bottomRight);
\coordinate (topLeft) at ($(bottomLeft) + (axis direction cs:0,0.02)$);
\coordinate (topRight) at ($(topLeft) + (axis direction cs:20,0)$);
\draw[red,thick] (topLeft) -- (topRight);
\end{axis}
\end{tikzpicture}
\end{document}