将单位转换为坐标适用于 x 坐标,但不适用于 y 坐标

将单位转换为坐标适用于 x 坐标,但不适用于 y 坐标

在杰克的帮助下,我(原则上)得到了这个问题“旧”版本的解决方案这里,即提取/转换数据中的单位为本地测量值。

它适用于X,但意外地失败了-协调。

为什么\pgfplotsconvertunittocoordinate对同一点两次不起作用?是否需要一些额外的代码?

图片

在此处输入图片描述

数据

http://pastebin.com/5AkHFZhh

平均能量损失

\documentclass[
a4paper
]{scrartcl}

\usepackage{
    lmodern,
    amsmath
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{
    tikz,
    pgfplots
    }
\usetikzlibrary{
    calc
}

\listfiles

\begin{document}

\begin{center}
\centering
\begin{tikzpicture}[font=\small]
\begin{axis}[
height=6cm,
width=14cm,
%
scale only axis=true,
xlabel={Distance in mm},
ylabel={Voltage in volt},
]
\addplot [sharp plot, no marks, x=Wegnormiert] table [col sep=tab] {data.txt} coordinate [pos=0.4] (A) coordinate [pos=0.6] (B);
\filldraw let \p1= (A) in (\x1,\y1) circle [radius=1pt] node[pin={270:({{\pgfplotsconvertunittocoordinate{x}{\x1}\pgfmathprintnumber[fixed,precision=1]{\pgfmathresult}}};{{\pgfplotsconvertunittocoordinate{y}{\y1}\pgfmathprintnumber[fixed,precision=1]{\pgfmathresult}}})}] {};
\draw (A) -| (current axis.west) coordinate (Ay) node[midway, above right=2pt, fill=white] {\(\simeq -2\)};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

答案1

为了让您开始行动,这里有一些替代方案:

有一个功能可以访问 周围任何物体的坐标pos\pgfplotspointplotattime{0.4}。这可能是最简单的方法,尽管它并不完全是人们想要的方式:

\documentclass{standalone}

\usepackage{
    lmodern,
    amsmath
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{
    tikz,
    pgfplots
    }
\usetikzlibrary{
    calc
}

\listfiles

\begin{document}

\begin{tikzpicture}[font=\small]
\begin{axis}[
height=6cm,
width=14cm,
%
scale only axis=true,
xlabel={Distance in mm},
ylabel={Voltage in volt},
]
\addplot [sharp plot, no marks, x=Wegnormiert] table [col sep=tab] {data.txt}
    coordinate[pos=0.4,pin={270:{%
      \pgfplotspointplotattime{0.4}%
      $(\pgfmathprintnumber[fixed,precision=1]
              {\pgfkeysvalueof{/data point/x}};
        \pgfmathprintnumber[fixed,precision=1]
            {\pgfkeysvalueof{/data point/y}})$
    }}] (A);
\filldraw let \p1= (A) in (\x1,\y1) circle [radius=1pt] ;
\draw (A) -| (current axis.west) coordinate (Ay) node[midway, above right=2pt, fill=white] {\(\simeq -2\)};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

此方法的详细信息请参阅手册中的“将节点放置在图的坐标上”部分pgfplots

@Jake 概述了替代解决方案(更接近您的预期)。对于您的图,类似(等效)的解决方案是将其添加disabledatascaling到轴的选项列表中。


话虽如此,您的问题实际上是功能请求“这是一个 PGF 点。请给我相关的高级坐标”。我已经在待办事项列表中做了记录pgfplots

该功能请求的实现将按照@Jake 的建议进行。

抱歉造成混淆\pgfplotsconvertunittocoordinate:此例程根本不应成为手册的一部分,因为它仅用于内部例程。一旦准备就绪,我将从手册中删除它并插入合适的函数。

答案2

发生这种情况的原因是 PGFPlots 会重新缩放并移动范围较小或较大的轴的单位。在这种情况下,x 轴未缩放,但 y 轴已移动并缩放。为了获得正确的值,您需要撤消转换。最简单的方法是将值传递给\pgfplotscoordmath{<axis>}{datascaletrafo inverse to fixed}{<value>}

\documentclass[
a4paper
]{scrartcl}

\usepackage{
    lmodern,
    amsmath
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{
    tikz,
    pgfplots
    }
\usetikzlibrary{
    calc
}

\listfiles

\begin{document}

\begin{center}
\centering
\begin{tikzpicture}[font=\small]
\begin{axis}[
height=6cm,
width=14cm,
%
scale only axis=true,
xlabel={Distance in mm},
ylabel={Voltage in volt},
]
\addplot [sharp plot, no marks, x=Wegnormiert] table {
0 -10
130 10
} coordinate [pos=0.4] (A) coordinate [pos=0.6] (B);
\filldraw let \p1= (A) in (\x1,\y1) circle [radius=1pt] node[pin={270:({{%
    \pgfplotsconvertunittocoordinate{x}{\x1}%
    \pgfplotscoordmath{x}{datascaletrafo inverse to fixed}{\pgfmathresult}%
    \pgfmathprintnumber[fixed,precision=1]{\pgfmathresult}}};{{%
    \pgfplotsconvertunittocoordinate{y}{\y1}%
    \pgfplotscoordmath{y}{datascaletrafo inverse to fixed}{\pgfmathresult}%
    \pgfmathprintnumber[fixed,precision=1]{\pgfmathresult}}})}] {};
\draw (A) -| (current axis.west) coordinate (Ay) node[midway, above right=2pt, fill=white] {\(\simeq -2\)};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

相关内容