用符号表示曲线交点的坐标来填充矩形下方的区域

用符号表示曲线交点的坐标来填充矩形下方的区域

我想填充具有原点和工作点的矩形下方的区域(Q_1, H_1)(Q_2, H_2)这些矩形分别由两条红色曲线与蓝色曲线的交点获得。我设法精确定位交点并绘制定义矩形的线段,然后我尝试使用,fill between但输出是线段和整个水平轴之间的彩色梯形。我开始认为缺失的元素是定义水平轴路径的区间的右端点(必须是且不是Q_1xmax,所以我寻找这个问题的解决方案,但没有任何相关的成功。我对这个包不是很熟悉(这就是为什么这个问题可能几乎是重复的但我无法确定的原因),所以我无法正确使用letpgfextractx函数来实现此目的。我如何符号化地引用交点的横坐标(OP1)并用它来正确定义有用的间隔fill between

以下是我的尝试:

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta,babel,calc,intersections}
\usepgfplotslibrary{fillbetween}

\begin{figure}[H] %% figure is closed
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=1, ymin=0, 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] {-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= ${(Q_1,H_1)}$ ] (OP1) at (intersection-1);
\path [name intersections={of=load2 and pump}]; 
\coordinate [label= ${(Q_2,H_2)}$ ] (OP2) at (intersection-1);

\draw[name path=opv1] (OP1) -- (OP1|-0,0);
\draw[name path=oph1] (OP1) -- (0,0 |- OP1);
\draw[name path=opv2] (OP2) -- (OP2|-0,0);
\draw[name path=oph2] (OP2) -- (0,0 |- OP2);

\path[name path=zero]
(\pgfkeysvalueof{/pgfplots/xmin},0) --
(\pgfkeysvalueof{/pgfplots/xmax},0);

\addplot[orange]fill between[of=oph1 and zero];
\end{axis}

\foreach \point in {OP1,OP2}
\fill [red] (\point) circle (2pt);


\end{tikzpicture}
\end{center}
\caption{System working point}
\end{figure}

在此处输入图片描述

答案1

因此,如果您需要的只是两个矩形,那么fill between就有点过于复杂了,您只需使用 即可\fill (x,y) rectangle (u,v);。您已经有坐标了。下面我还添加了backgrounds库,并将矩形放在scope带有 的环境中[on background layer],以便矩形位于情节线后面。

我使用了\filldraw,但如果您不想绘制边框,请将其更改为\fill,然后删除该draw=black选项。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta,babel,calc,intersections,backgrounds}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=1, ymin=0, 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] {-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= ${(Q_1,H_1)}$ ] (OP1) at (intersection-1);
\path [name intersections={of=load2 and pump}]; 
\coordinate [label= ${(Q_2,H_2)}$ ] (OP2) at (intersection-1);

\begin{scope}[on background layer]
\filldraw[fill=orange!50,draw=black] (0,0) rectangle node {foo} (OP2);
\filldraw[fill=blue!80!red!50!white,draw=black] (0,0-|OP2) rectangle node {bar} (OP1);
\end{scope}
\end{axis}

\foreach \point in {OP1,OP2}
  \fill [red] (\point) circle (2pt);

\end{tikzpicture}
\end{document}

答案2

正如 Torbjørn T. 和 Gernot 所说,我也不能 100% 确定你真正想要实现什么。但我猜答案是Torbjørn T.几乎就是您正在寻找的内容。

因此,这几乎是相同的答案,但是使用了这些fill between功能。

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{
        intersections,
        pgfplots.fillbetween,
    }
    \pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=0, xmax=1, ymin=0, ymax=1.5,
        axis x line=middle,
        axis y line=middle,
        xlabel=$Q$,ylabel=$H$,
        ticks=none,
    ]
        \addplot[blue,domain=0:1,name path=pump] {-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= ${(Q_1,H_1)}$ ] (OP1) at (intersection-1);
        \path [name intersections={of=load2 and pump}];
            \coordinate [label= ${(Q_2,H_2)}$ ] (OP2) at (intersection-1);

        \draw [name path=opv1] (OP1) -- (OP1 |- 0,0);
        \draw [name path=oph1] (OP1) -- (0,0 |- OP1);
        \draw [name path=opv2] (OP2) -- (OP2 |- 0,0);
        \draw [name path=oph2] (OP2) -- (0,0 |- OP2);

        \path [name path=zero]
            (\pgfkeysvalueof{/pgfplots/xmin},0) --
            (\pgfkeysvalueof{/pgfplots/xmax},0);

        \addplot [orange] fill between [
            of=oph1 and zero,
            % -----------------------------------------------------------------
            % in order to achieve the desired result you can add a `soft clip'
            % path which cuts off the unwanted rest not inside of this `soft
            % clip' path and you need (in this case) also add `reverse=true'
            % explicitly, otherwise you get another unwanted result
            reverse=true,
            soft clip={
                % (depending on what you exactly need use one the following
                % starting corrdinates)
%                (OP2 |- 0,\pgfkeysvalueof{/pgfplots/ymin})
                (\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin})
                    rectangle
                (OP1)
            },
            % -----------------------------------------------------------------
        ];

        % add some text centered in the rectangle (as requested in the comments)
        \path
%            % (to show how it is drawn ...)
%            [draw,dashed]
                (\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin})
                    -- node [pos=0.5] {some text} (OP1);
        ;

    \end{axis}

    \foreach \point in {OP1,OP2} {
        \fill [red] (\point) circle (2pt);
    }

\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

答案3

PSTricks 解决方案使用pst-plot包裹:

\documentclass{article}

\usepackage{pst-plot}

% The blue-coloured graph.
\def\aA{-0.5}
\def\bA{0}
\def\cA{1}
\def\fA(#1){(\aA*(#1)^2+\bA*(#1)+\cA)}

% The "first" red-coloured graph.
\def\aB{2}
\def\bB{1.6}
\def\cB{0.5}
\def\fB(#1){(\aB*(#1)^2+\bB*(#1)+\cB)}

% The "second" red-coloured graph.
\def\aC{0.5}
\def\bC{0.4}
\def\cC{0.5}
\def\fC(#1){(\aC*(#1)^2+\bC*(#1)+\cC)}

% Intersection points, x-coordinates.
\def\xAB{\fpeval{(-(\bA-\bB)-sqrt((\bA-\bB)^2-4*(\aA-\aB)*(\cA-\cB)))/(2*(\aA-\aB))}}
\def\xAC{\fpeval{(-(\bA-\bC)-sqrt((\bA-\bC)^2-4*(\aA-\aC)*(\cA-\cC)))/(2*(\aA-\aC))}}

\begin{document}

{\psset{
   xunit = 6,
   yunit = 3,
   dimen = m,
   algebraic
 }
\begin{pspicture}(1.2,1.5)
 {\psset{fillstyle = solid}
  \psframe[
    fillcolor = orange!60
  ](0,0)(\xAB,\fpeval{\fA(\xAB)})
  \rput(\fpeval{0.5*\xAB},\fpeval{0.5*\fA(\xAB)}){cat}
  \psframe[
    fillcolor = blue!60
  ](\xAB,0)(\xAC,\fpeval{\fA(\xAC)})
  \rput(\fpeval{0.5*(\xAC+\xAB)},\fpeval{0.5*\fA(\xAC)}){dog}}
  \psaxes[
    ticks = none,
    labels = none
  ]{->}(0,0)(-0.05,-0.1)(0.8,1.5)[$Q$,120][$H$,330]
  \psplot[
    linecolor = blue
  ]{0}{0.8}{\fA(x)}
  \psplot[
    linecolor = red
  ]{0}{0.4}{\fB(x)}
  \psplot[
    linecolor = red
  ]{0}{0.8}{\fC(x)}
  \psdots[
    dotstyle = o,
    fillcolor = red
  ](\xAB,\fpeval{\fA(\xAB)})(\xAC,\fpeval{\fA(\xAC)})
\end{pspicture}}

\end{document}

输出

您所要做的就是更改三个二次多项式的系数值,绘图就会相应调整。(如果需要,您必须手动更改绘图范围和轴范围。)

添加

如果你添加

\uput[90](\xAB,\fpeval{\fA(\xAB)}){$(G_{1},H_{1})$}
\uput[90](\xAC,\fpeval{\fA(\xAC)}){$(G_{2},H_{2})$}

到绘图中,你会得到两个交点的标签。

相关内容