在此代码中:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta,babel,calc,intersections,backgrounds}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=2, ymin=-0.1, ymax=1.5,
axis x line=middle,
axis y line=middle,
xlabel=$Q$,ylabel=$H$,
ticks=none,
]
\addplot[name path=pump,blue,domain=0:1.5] {-0.5*x^2+1};
\addplot[red,domain=0:1,name path=load1] {0.5*x^2+0.4*x+0.5};
\addplot[red,domain=0:1,name path=load2] {2*x^2+1.6*x+0.5};
\path [name intersections={of=load1 and pump}];
\coordinate [label= $A$ ] (OP1) at (intersection-1);
\path [name intersections={of=load2 and pump}];
\coordinate [label= $B$ ] (OP2) at (intersection-1);
%\path [name intersections={of=pump and axis}];
%\coordinate [label= $C$ ] (OP3) at (intersection-1);
\end{axis}
\foreach \point in {OP1,OP2}
\fill [red] (\point) circle (2pt);
\end{tikzpicture}
\end{document}
(改编自一个更复杂的例子) 我想在名为 line 的线和 x 轴OP3
的交点处创建一个坐标,以便我也可以在那里使用。pump
\fill [red] (\point) circle (2pt)
注释行
%\path [name intersections={of=pump and axis}];
%\coordinate [label= $C$ ] (OP3) at (intersection-1);
代表一次失败的尝试。两者都不{of=pump and xaxis}
工作{of=pump x-axis}
。
pump
如何引用和 x 轴的交点?
答案1
您需要命名 x 轴,例如
x axis line style={name path=<name>}, % <--- in axis preamble
然后你可以写:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta,
babel,backgrounds,
calc,
intersections}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill=red, inner sep=2pt}
]
\begin{axis}[
axis lines=middle,
x axis line style={name path=xaxis}, % <---
xmin=0, xmax=2,
ymin=-0.1, ymax=1.5,
xlabel=$Q$,ylabel=$H$,
ticks=none,
]
\addplot[name path=pump,blue,domain=0:1.5] {-0.5*x^2+1};
\addplot[red,domain=0:1,name path=load1] {0.5*x^2+0.4*x+0.5};
\addplot[red,domain=0:1,name path=load2] {2*x^2+1.6*x+0.5};
\path [name intersections={of=load1 and pump, by=a}]
node[dot, label= $A$] at (a) {} ;
\path [name intersections={of=load2 and pump, by=b}]
node[dot, label= $B$] at (b) {} ;
\path [name intersections={of=pump and xaxis, by=c}]
node[dot, label= $C$] at (c) {};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
您只需将名称添加到轴线即可,无需重新绘制它(这需要您知道其精确位置)。这可以通过添加
every inner x axis line/.append style={name path=x-axis}
顺便说一句,fillbetween
加载intersections
。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.17} %<-consider adding
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[bullet/.style={circle,fill,red,inner sep=0pt,minimum width=3pt}]
\begin{axis}[
xmin=0, xmax=2, ymin=-0.1, ymax=1.5,
axis x line=middle,
axis y line=middle,
xlabel=$Q$,ylabel=$H$,
ticks=none,
every inner x axis line/.append style={name path=x-axis}
]
\addplot[name path=pump,blue,domain=0:1.5] {-0.5*x^2+1};
\addplot[red,domain=0:1,name path=load1] {0.5*x^2+0.4*x+0.5};
\addplot[red,domain=0:1,name path=load2] {2*x^2+1.6*x+0.5};
\path [name intersections={of=load1 and pump}]
(intersection-1) coordinate [bullet,label= $A$ ] (OP1)
[name intersections={of=load2 and pump}]
(intersection-1) coordinate [bullet,label= $B$ ] (OP2)
[name intersections={of=pump and x-axis}]
(intersection-1) coordinate [bullet,label= $C$] (OP3);
\end{axis}
\end{tikzpicture}
\end{document}