使用交点绘制的线在组图中绘制在错误的图中。我该如何纠正?

使用交点绘制的线在组图中绘制在错误的图中。我该如何纠正?

我的代码如下所示。在评论中,我还指出了我遇到的问题。我正在使用出色的 tikz 和 pgfplots 创建 1row x 2cols 组图。我确定(我认为)第二个图中的交点,并尝试在那里绘制一条线和一个节点。请查看名为pline10和的路径mktddcurve。但输出出现在第一个图中而不是第二个图中。我不明白为什么,也不知道如何纠正它。我真的很感激一些帮助。提前谢谢您。

代码

\documentclass[class=extarticle,tikz,border=0.5cm,14pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\usetikzlibrary{calc,intersections}
\usepgfplotslibrary{groupplots}


\tikzset{every picture/.style={line width=1pt}}

\definecolor{asparagus}{rgb}{0.53, 0.66, 0.42}
\definecolor{arsenic}{rgb}{0.23, 0.27, 0.29}
\definecolor{bittersweet}{rgb}{1.0, 0.44, 0.37}
\definecolor{bleudefrance}{rgb}{0.19, 0.55, 0.91}
\definecolor{bluegray}{rgb}{0.4, 0.6, 0.8}
\definecolor{brickred}{rgb}{0.8, 0.25, 0.33}

\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[
        group style={group size=2 by 1},
        width=4in,
        height=4in,
        xlabel={Quantity},
        axis x line=middle,
        axis y line=middle,
        every axis/.style={color=arsenic},
        every axis x label/.style=
            {at={(ticklabel cs: 0.65,0.2cm)}, anchor=north,color=arsenic},
        every axis y label/.style=
            {at={(ticklabel cs: 0.85,0.4cm)}, rotate=90},
        ticks=none,
        ymin=0,
        ymax=40,
        xmin=0,
        xmax=12,
        clip mode=individual
    ]

        %% firm panel
        \nextgroupplot[ylabel={Price, Costs}, xlabel={Quantity of a Firm}]
            \addplot[name path=ATC, mark=none,smooth,samples=100,domain=1:10,color=bittersweet]
            {x*x/3 - 5*x/2 + 20/x + 10} node[label={[xshift=2pt,yshift=-5pt]ATC}] {};

            \addplot[name path=MC, mark=none,smooth,samples=100,domain=3:7.5,color=asparagus]
                    {x*x - 5*x + 10} node[label={[xshift=2pt,yshift=-5pt]MC}] {};

            \node[xshift=-10pt,color=arsenic] at (0,13) {10}; 
            \coordinate (p10firm) at (0,13);



        %% market panel
        \nextgroupplot[ylabel={Price}, xlabel={Quantity in the Market}]
        \addplot[mark=none,smooth,samples=100,domain=3:7.5,color=asparagus]{x*x - 5*x + 10} 
                    node[label={[xshift=2pt,yshift=-5pt]S}] {};

        %% ** path mktddcurve is defined here **
        \draw[name path=mktddcurve, color=brickred] plot [smooth] coordinates {(1, 28) (5.54, 13) (8, 6)}
                    node[label={[xshift=0.3cm,yshift=-0.6cm]D}]{};

        \coordinate (p10market) at (10,13); 

    \end{groupplot}
    %% **path pline10 is defined here**
    \draw[name path=pline10, thin,dashed,color=arsenic] (p10firm) -- (p10market); 

    %% **What I don't understand: Why is the following line and label appearing in the 
    %% the left panel an not in the right panel. How do I correct this?**
    \path [name intersections={of=pline10 and mktddcurve, by=eqm}];
    \draw[thin, dashed, color=arsenic] let \p{A}=(eqm) in (eqm) -- (\x{A}, 0);
    \node at (eqm) {E};

\end{tikzpicture}
\end{document}

答案1

每当您离开某个环境(如 groupplot)时,您都会丢失所有局部定义。命名坐标会被保存(全局),但其他内容则不会。

\documentclass[class=extarticle,tikz,border=0.5cm,14pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\usetikzlibrary{calc,intersections}
\usepgfplotslibrary{groupplots}


\tikzset{every picture/.style={line width=1pt}}

\definecolor{asparagus}{rgb}{0.53, 0.66, 0.42}
\definecolor{arsenic}{rgb}{0.23, 0.27, 0.29}
\definecolor{bittersweet}{rgb}{1.0, 0.44, 0.37}
\definecolor{bleudefrance}{rgb}{0.19, 0.55, 0.91}
\definecolor{bluegray}{rgb}{0.4, 0.6, 0.8}
\definecolor{brickred}{rgb}{0.8, 0.25, 0.33}

\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[
        group style={group size=2 by 1},
        width=4in,
        height=4in,
        xlabel={Quantity},
        axis x line=middle,
        axis y line=middle,
        every axis/.style={color=arsenic},
        every axis x label/.style=
            {at={(ticklabel cs: 0.65,0.2cm)}, anchor=north,color=arsenic},
        every axis y label/.style=
            {at={(ticklabel cs: 0.85,0.4cm)}, rotate=90},
        ticks=none,
        ymin=0,
        ymax=40,
        xmin=0,
        xmax=12,
        clip mode=individual
    ]

        %% firm panel
        \nextgroupplot[ylabel={Price, Costs}, xlabel={Quantity of a Firm}]
            \addplot[name path=ATC, mark=none,smooth,samples=100,domain=1:10,color=bittersweet]
            {x*x/3 - 5*x/2 + 20/x + 10} node[label={[xshift=2pt,yshift=-5pt]ATC}] {};

            \addplot[name path=MC, mark=none,smooth,samples=100,domain=3:7.5,color=asparagus]
                    {x*x - 5*x + 10} node[label={[xshift=2pt,yshift=-5pt]MC}] {};

            \node[xshift=-10pt,color=arsenic] at (0,13) {10}; 
            \coordinate (p10firm) at (0,13);



        %% market panel
        \nextgroupplot[ylabel={Price}, xlabel={Quantity in the Market}]
        \addplot[mark=none,smooth,samples=100,domain=3:7.5,color=asparagus]{x*x - 5*x + 10} 
                    node[label={[xshift=2pt,yshift=-5pt]S}] {};

        %% ** path mktddcurve is defined here **
        \draw[name path=mktddcurve, color=brickred] plot [smooth] coordinates {(1, 28) (5.54, 13) (8, 6)}
                    node[label={[xshift=0.3cm,yshift=-0.6cm]D}]{};

        \coordinate (p10market) at (10,13); 
        \path[name path=pline10] (p10firm) -- (p10market);% will be cropped if drawn here
        \path [name intersections={of=pline10 and mktddcurve, by=eqm}];

    \end{groupplot}
    \draw[thin,dashed,color=arsenic] (p10firm) -- (p10market); 
    \draw[thin, dashed, color=arsenic] let \p{A}=(eqm) in (eqm) -- (\x{A}, 0);
    \node at (eqm) {E};

\end{tikzpicture}
\end{document}

演示

相关内容