pgfplots log10 图在 0 附近停止

pgfplots log10 图在 0 附近停止

我正在尝试使用以下代码绘制 lg(x):

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
    \centering\begin{tikzpicture}
        \begin{axis}[
            axis lines=center,
            xmin = -1.2,
            ymin = -5.2,
            xmax = 5.2,
            ymax = 5.2,
            ylabel=$y$,
            xlabel=$x$,
            ]
            \addplot [domain=-1:5,samples=1000,thick, blue ] {log10(x)}
            node [pos=0.6, above left] {$f(x)=\lg x$};
        \end{axis}
    \end{tikzpicture}
\end{document}

但图表并没有像它应该的那样一直延伸到最底部。我该如何改变这种现象?

答案1

该值仅以 1000 个点计算,这还不够接近 y 轴。samples您无需将值增加到荒谬的值,只需像这样自己将点拉低即可:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
    \centering\begin{tikzpicture}
        \begin{axis}[
            axis lines=center,
            xmin = -1.2,
            ymin = -5.2,
            xmax = 5.2,
            ymax = 5.2,
            ylabel=$y$,
            xlabel=$x$,
            ]
            \addplot [domain=-1:5,samples=1000,thick, blue,y filter/.expression={y<-2?\pgfkeysvalueof{/pgfplots/ymin}:y}] {log10(x)}
            node [pos=0.8, above left] {$f(x)=\lg x$};
        \end{axis}
    \end{tikzpicture}
\end{document}

图形

您还可以通过创造性地分配样本来获得相同的结果,例如:

samples at ={0.00000001,0.01,0.02,...,5}

相关内容