如何将 PGFplots 中的交叉点用作 x 刻度?

如何将 PGFplots 中的交叉点用作 x 刻度?

我对 PGFplots 还很陌生,但我已经非常喜欢它了!

我想要一个图,其中两条曲线的交点或数据达到某个 y 值时在 x 轴上标记为 x 刻度。可以将带有文本的节点放在 x 轴附近,但 x 刻度之间的间距会关闭、重叠,并且位置永远不会与实际的 x 刻度位置相同。有没有办法extra x ticks在代码中定义另一个,即在轴的选项之后?

另外,当我使用时axis y line = leftaxis x line = bottom如何确保箭头上方没有刻度标记?

此外,有人可以向我解释一下虚线曲线图中的含义吗|-,我尝试了 stackexchange 中的不同代码来获得我目前的位置。

\draw [dashed, name intersections={of=y-value and line2, by={intersect2}}] % What does the description of the coordinates with |- mean actually? (intersect2) -- (intersect2 |- 0,0);

以下是示例图的图片: PLOT

还有一个可编译的例子:

\documentclass[a4paper,11pt,twoside]{scrreprt}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}


\usepackage{graphicx}


\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{intersections}

\begin{document}


\begin{figure}
\centering

\begin{tikzpicture}[baseline]

\begin{axis}[
xlabel= x,
ylabel = y,
axis x line = bottom,
axis y line = left,
xmin=0, xmax=7,
ymin = 0, ymax = 2.5,
xtick = \empty,
extra x ticks = {0, 3, 6},
every axis plot/.append style={semithick},
%% Clipping needs to be disabled, otherwise I cannot draw the text
clip=false,
]
\addplot[name path global=y-value,gray, ultra thin] coordinates{(0,2) (7,2)};
\addplot[name path global=line1] coordinates{(0,0) (5,2.5)};
\addplot[name path global=line2] coordinates{(0,0) (4.7,2.5)};
\addplot[name path global=line3] coordinates{(0,0) (3,2.5)};
%
% Here I start with the code for intersections of the different lines
\draw [dashed, name intersections={of=y-value and line1, by={intersect1}}]
(intersect1) -- (intersect1 |- 0,0);
% The value -0.15 is determined by trial and error for every plot. It is never 
% perfectly at the same height as the x-ticks though.
% Especially when I change the size of the plot.
\node at (intersect1|- 0,-0.15 ) {\pgfplotspointgetcoordinates{(intersect1)}
    $
    \pgfmathprintnumber[fixed, precision=1]{\pgfkeysvalueof{/data point/x}}
    $};
%----   
\draw [dashed, name intersections={of=y-value and line2, by={intersect2}}]
(intersect2) -- (intersect2 |- 0,0);
%
\node at (intersect2|- 0,-0.15 ) {\pgfplotspointgetcoordinates{(intersect2)}
    $
    \pgfmathprintnumber[fixed, precision=1]{\pgfkeysvalueof{/data point/x}}
    $};
%-----
\draw [dashed, name intersections={of=y-value and line3, by={intersect3}}]
(intersect3) -- (intersect3 |- 0,0);
%
\node at (intersect3|- 0,-0.15 ) {\pgfplotspointgetcoordinates{(intersect3)}
    $
    \pgfmathprintnumber[fixed, precision=1]{\pgfkeysvalueof{/data point/x}}
    $};
%   
\end{axis}                                  
\end{tikzpicture}
\end{figure}

\end{document}

答案1

如果你真的想使用,[extra x ticks={...}]你需要将屏幕坐标转换为轴。此外,你需要在轴之前准备好额外的刻度,这可以通过在顶部叠加具有相同尺寸的第二个轴来实现。

另一方面,它们仍然会重叠。

\documentclass[a4paper,11pt,twoside]{scrreprt}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage{graphicx}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{intersections}

\newlength{\xscale}
\newlength{\xcoord}

\begin{document}


\begin{figure}
\centering

\begin{tikzpicture}[baseline]

\begin{axis}[
xlabel= x,
ylabel = y,
axis x line = bottom,
axis y line = left,
xmin=0, xmax=7,
ymin = 0, ymax = 2.5,
xtick = {0, 3, 6},
%extra x ticks = {0, 3, 6},
every axis plot/.append style={semithick},
%% Clipping needs to be disabled, otherwise I cannot draw the text
clip=false,
]
\addplot[name path global=y-value,gray, ultra thin] coordinates{(0,2) (7,2)};
\addplot[name path global=line1] coordinates{(0,0) (5,2.5)};
\addplot[name path global=line2] coordinates{(0,0) (4.7,2.5)};
\addplot[name path global=line3] coordinates{(0,0) (3,2.5)};
%
% Here I start with the code for intersections of the different lines
\draw [dashed, name intersections={of=y-value and line1, by={intersect1}}]
(intersect1) -- (intersect1 |- 0,0);
\draw [dashed, name intersections={of=y-value and line2, by={intersect2}}]
(intersect2) -- (intersect2 |- 0,0);
%
\draw [dashed, name intersections={of=y-value and line3, by={intersect3}}]
(intersect3) -- (intersect3 |- 0,0);
%
\coordinate (scale) at (1,0);% assume lower left at (0,0)
\pgfplotsextra{% wait until scale determined
  \pgfextractx{\xscale}{\pgfpointanchor{scale}{center}}%
  \pgfextractx{\xcoord}{\pgfpointanchor{intersect1}{center}}%
  \pgfmathdivide{\xcoord}{\xscale}%
  \xdef\ticks{\pgfmathresult}%
  \pgfextractx{\xcoord}{\pgfpointanchor{intersect2}{center}}%
  \pgfmathdivide{\xcoord}{\xscale}%
  \xdef\ticks{\ticks,\pgfmathresult}%
  \pgfextractx{\xcoord}{\pgfpointanchor{intersect3}{center}}%
  \pgfmathdivide{\xcoord}{\xscale}%
  \xdef\ticks{\ticks,\pgfmathresult}%
}%
\end{axis}%
\begin{axis}[% overlay new axis on top of old
axis x line = bottom,
axis y line = left,
xmin=0, xmax=7,
ymin = 0, ymax = 2.5,
xtick=\empty,
ytick=\empty,
extra x ticks = {\ticks}
]\end{axis}  
\end{tikzpicture}
\end{figure}

\end{document}

demo

答案2

这是对更自动化版本的尝试。我使用它\pgfplotsinvokeforeach来避免重复。然后我以不干扰的方式添加标签。您可以通过重新定义坐标来将它们上下移动。标签的(L)最大值x和它们之间的距离都是硬编码的。人们可以很容易地改进这一点,但为此我需要更多关于这些标签在“现实生活情况”中存在多少的输入。

\documentclass[a4paper,11pt,twoside]{scrreprt}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{intersections}

\begin{document}

\begin{figure}
\centering

\begin{tikzpicture}[baseline]

\begin{axis}[
xlabel= x,
ylabel = y,
axis x line = bottom,
axis y line = left,
xmin=0, xmax=7,
ymin = 0, ymax = 2.5,
xtick = \empty,
extra x ticks = {0, 3, 6},
every axis plot/.append style={semithick},
%% Clipping needs to be disabled, otherwise I cannot draw the text
clip=false,
]
\addplot[name path global=y-value,gray, ultra thin] coordinates{(0,2) (7,2)};
\addplot[name path global=line-1] coordinates{(0,0) (5,2.5)};
\addplot[name path global=line-2] coordinates{(0,0) (4.7,2.5)};
\addplot[name path global=line-3] coordinates{(0,0) (3,2.5)};
%
% Here I start with the code for intersections of the different lines
\path (0,0) coordinate (O) (0,-0.6) coordinate (L);
%   
\pgfplotsinvokeforeach{1,2,3}{%
\draw [dashed, name intersections={of=y-value and line-#1, by={intersect-#1}}]
(intersect-#1) -- (intersect-#1 |- O) coordinate (X-#1);
\node (L-#1) at (5-#1,0 |- L){\pgfplotspointgetcoordinates{(intersect-#1)}
    $
    \pgfmathprintnumber[fixed, precision=1]{\pgfkeysvalueof{/data point/x}}
    $};
\draw[-latex] (L-#1) to[out=90,in=-90] (X-#1);
}                        
\end{axis}    

\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

相关内容