在同一坐标系中结合 pow 和对数图

在同一坐标系中结合 pow 和对数图

我在同一个坐标系中绘制两个图形时遇到了麻烦。我可以单独绘制它们,但不能把它们放在一起:

\usetikzlibrary{
        calc,
        patterns,
        positioning
    }
    \pgfplotsset{
        compat=1.16,
        samples=200,
        clip=false,
        my axis style/.style={
            axis x line=middle,
            axis y line=middle,
            legend pos=outer north east,
            axis line style={
                ->,
            },
            legend style={
                font=\footnotesize
            },
            label style={
                font=\footnotesize
            },
            tick label style={
                font=\footnotesize
            },
            xlabel style={
                at={
                    (ticklabel* cs:1)
                },
                anchor=west,
                font=\footnotesize,
            },
            ylabel style={
                at={
                    (ticklabel* cs:1)
                },
                anchor=west,
                font=\footnotesize,
            },
            xlabel=$X$,
            ylabel=$Y$
        },
    }
    \tikzset{
        >=stealth
    }
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
  \begin{axis}[my axis style,
  width=\textwidth,
  height=\textwidth,
    restrict x to domain=0:10,restrict y to domain=0:5,
    enlargelimits
      ]
    \addplot[domain=0:5,
    thick
    ] 
    {ln(20*x + 1)/ln(10)}; 
\end{axis}
\end{tikzpicture}
\end{figure}

\begin{figure}[h]
\centering
\begin{tikzpicture}
  \begin{axis}[my axis style,
  width=\textwidth,
  height=\textwidth
  ]
    \addplot[domain=2.73:5,
    thick
    ] 
    {10^x -1 / 20};
  \end{axis}
\end{tikzpicture}
\end{figure} 

在此处输入图片描述

最终的表示应该是这样的,但我肯定漏掉了一些让其工作的东西。如果有其他绘制切线箭头的技巧,也非常欢迎。

目标

如果两个图位于同一轴环境中,则会产生以下结果:

\usetikzlibrary{
        calc,
        patterns,
        positioning
    }
    \pgfplotsset{
        compat=1.16,
        samples=200,
        clip=false,
        my axis style/.style={
            axis x line=middle,
            axis y line=middle,
            legend pos=outer north east,
            axis line style={
                ->,
            },
            legend style={
                font=\footnotesize
            },
            label style={
                font=\footnotesize
            },
            tick label style={
                font=\footnotesize
            },
            xlabel style={
                at={
                    (ticklabel* cs:1)
                },
                anchor=west,
                font=\footnotesize,
            },
            ylabel style={
                at={
                    (ticklabel* cs:1)
                },
                anchor=west,
                font=\footnotesize,
            },
            xlabel=$X$,
            ylabel=$Y$
        },
    }
    \tikzset{
        >=stealth
    }
\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}
  \begin{axis}[my axis style,
  width=\textwidth,
  height=\textwidth,
  restrict x to domain=0:5, restrict y to domain=0:10^4,
    enlargelimits
      ]
    \addplot[domain=0:5,
    thick
    ] 
    {ln(20*x + 1)/ln(10)}; 
    \addplot[domain=2.73:5,
    thick
    ] 
    {10^x -1 / 20};
\end{axis}
\end{tikzpicture}
\end{figure}

第一张图消失

答案1

我只会选择不同的函数,对它们进行规范化是使它们具有相同比例的简单方法。首先请参见tikzpicture下面的代码。我对切线没有任何好主意,尽管搜索可能会导致之前关于绘制切线的问题。也就是说,从您展示的图像中,您真正想要的是两个函数的导数相等的切线。因此,一种选择是计算两个函数的导数,找到它们相等的 x 值,并确定通过函数上点的切线方程。完成后(例如使用 Wolfram alpha 或笔和纸),您可以执行\addplot [samples at={x1, x2}, <->] {a*x + b};,其中 x1 和 x2 是切点周围的值,a、b 是您之前确定的值。)

另一个选项是制作两个轴,并将第二个轴放在第一个轴的上方。请参阅tikzpicture下面代码中的第二个。您可能想要调整域、轴限值等,但我想您已经了解了大致思路。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.16,
        samples=100,
        clip=false,
        my axis style/.style={
            axis x line=middle,
            axis y line=middle,
            legend pos=outer north east,
            axis line style={
                ->,
            },
            legend style={
                font=\footnotesize
            },
            label style={
                font=\footnotesize
            },
            tick label style={
                font=\footnotesize
            },
            xlabel style={
                at={
                    (ticklabel* cs:1)
                },
                anchor=west,
                font=\footnotesize,
            },
            ylabel style={
                at={
                    (ticklabel* cs:1)
                },
                anchor=west,
                font=\footnotesize,
            },
            xlabel=$X$,
            ylabel=$Y$
        },
    }
    \tikzset{
        >=stealth
    }
\begin{document}


First option: choose different functions, normalized at a useful x-value.

\begin{tikzpicture}[
  declare function={
    f(\x)=ln(2.2*\x + 1)/ln(10); % note different functions, which look more like the sketch
    g(\x)=2.2^\x -1;
    c(\x)=f(\x)/f(4.7); % normalize the functions so they are 1 at x=4.85
    u(\x)=g(\x)/g(4.7);
    }
]
  \begin{axis}[my axis style,
  width=0.5\textwidth,
  height=0.5\textwidth,
  domain=0:5,
  restrict y to domain=0:1.1,
  enlargelimits,
  xtick=\empty,ytick=\empty,
      ]
    \addplot[
    thick
    ] 
    {c(x)} node[right] {$c(X)$}; 
    \addplot[
    thick
    ] 
    {u(x)} node[above] {$u(X)$}; 
    
\addplot[ycomb,samples at={4.7}, very thin, gray] {c(x)};
\end{axis}
\end{tikzpicture}


Second option: two axes one on top of the other.

\begin{tikzpicture}
  \begin{axis}[my axis style,
  width=0.5\textwidth,
  height=0.5\textwidth,
  restrict y to domain=0:10^4,
  enlargelimits,
  name=ax1, % name first axis
  xtick=\empty,ytick=\empty % remove ticks 
      ]
   \addplot[domain=0:5,
    thick
    ] 
    {ln(20*x + 1)/ln(10)};  
\end{axis}
  \begin{axis}[my axis style,
  width=0.5\textwidth,
  height=0.5\textwidth,
  restrict y to domain=0:10^4,
  enlargelimits,
  % place the second axis with its origin ...
  anchor=origin,
  % at the origin of the first axis
  at={(ax1.origin)}, 
  % remove axis lines, ticks, etc.
  hide axis
      ]
   \addplot[domain=0:5,
   thick
   ] 
   {10^x -1 / 20};
\end{axis}

\end{tikzpicture}
\end{document}

相关内容