TikZ:找到样本的正确值

TikZ:找到样本的正确值

你能帮我找到正确的参数值samples吗?我想获得一条通过横坐标轴截距的平滑曲线。

这是我的三次尝试:samples范围 50-140 产生“尺寸太大”错误

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{float}
\begin{document}
\begin{figure}[H]
%\captionof{figure}{Metodo }
\centering
\begin{tikzpicture}
  \begin{axis}[
  legend pos=outer north east,
  legend cell align={left},
  xmax=8,xmin=0,
  ymax=1,ymin=0,
  yticklabels=\empty, xticklabels=\empty,
  width=8cm,
  height=6cm,
  axis lines = middle,
  set layers,
  x label style={at={(1,0)},right},
  y label style={at={(0,1)},above},
  xlabel={$T$},ylabel={$\xi$},
  y tick style={draw=none}, x tick style={draw=none},
  style={thick}
               ]              
        \addplot [blue,smooth,very thick,domain=0:7,samples=140] 
        {1-( (0.1*exp(-1/4)) *exp(1/x))};
        %\addplot[only marks, mark=*,fill=yellow] coordinates { (0,1.5) (2,2.3) (4,3.1) (6,3.9) (8,4.7) };
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
  \begin{axis}[
  legend pos=outer north east,
  legend cell align={left},
  xmax=8,xmin=0,
  ymax=1,ymin=0,
  yticklabels=\empty, xticklabels=\empty,
  width=8cm,
  height=6cm,
  axis lines = middle,
  set layers,
  x label style={at={(1,0)},right},
  y label style={at={(0,1)},above},
  xlabel={$T$},ylabel={$\xi$},
  y tick style={draw=none}, x tick style={draw=none},
  style={thick}
               ]              
        \addplot [blue,smooth,very thick,domain=0:7,samples=50] 
        {1-( (0.1*exp(-1/4)) *exp(1/x))};
        %\addplot[only marks, mark=*,fill=yellow] coordinates { (0,1.5) (2,2.3) (4,3.1) (6,3.9) (8,4.7) };
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
  \begin{axis}[
  legend pos=outer north east,
  legend cell align={left},
  xmax=8,xmin=0,
  ymax=1,ymin=0,
  yticklabels=\empty, xticklabels=\empty,
  width=8cm,
  height=6cm,
  axis lines = middle,
  set layers,
  x label style={at={(1,0)},right},
  y label style={at={(0,1)},above},
  xlabel={$T$},ylabel={$\xi$},
  y tick style={draw=none}, x tick style={draw=none},
  style={thick}
               ]              
        \addplot [blue,smooth,very thick,domain=0:7,samples=20] 
        {1-( (0.1*exp(-1/4)) *exp(1/x))};
        %\addplot[only marks, mark=*,fill=yellow] coordinates { (0,1.5) (2,2.3) (4,3.1) (6,3.9) (8,4.7) };
    \end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

有没有比使用samples参数更好的解决方案?

答案1

您的主要问题不是样本数量;而是计算出的负值的幅度极大X趋近于零。

我建议使用restrict y to domain=-10:1加上一些其他的改进(0.1*exp(-1/4)只计算一次常数,绘制渐近线时X→ +∞,X轴标签位置,删除未使用的选项……)。

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\myConstFactor}{0.1*exp(-1/4)}% compute this only once
  \begin{axis}[
  ymax=1.5,
  extra y ticks={1},
  restrict y to domain=-10:1,
  width=8cm,
  height=6cm,
  axis lines = middle,
  xlabel={$T$}, ylabel={$\xi$},
  x label style={
    at={(current axis.right of origin)},
    anchor=west,
  },
  y label style={
    at={(current axis.above origin)},
    anchor=south,
  },
  style={thick}
  ]
  \addplot[blue, smooth, very thick, domain=0:7, samples=150]
    {1 - \myConstFactor*exp(1/x)};
  % Draw the asymptote for x → +∞
  \draw[red!80] (\pgfkeysvalueof{/pgfplots/xmin}, 1) --
                (\pgfkeysvalueof{/pgfplots/xmax}, 1);
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

由于您只想显示具有非负纵坐标的点,我们可以使用restrict y to domain=0:1。请注意,这次绘图从正横坐标开始。

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\myConstFactor}{0.1*exp(-1/4)}% compute this only once
  \begin{axis}[
  ymax=1.1,
  extra y ticks={1},
  restrict y to domain=0:1,
  width=8cm,
  height=6cm,
  axis lines = middle,
  xlabel={$T$}, ylabel={$\xi$},
  x label style={
    at={(current axis.right of origin)},
    anchor=west,
  },
  y label style={
    at={(current axis.above origin)},
    anchor=south,
  },
  style={thick},
  ]
  \addplot[blue, smooth, very thick, domain=0:7, samples=350]
    {1 - \myConstFactor*exp(1/x)};
  % Draw the asymptote for x → +∞
  \draw[red!80] (\pgfkeysvalueof{/pgfplots/xmin}, 1) --
                (\pgfkeysvalueof{/pgfplots/xmax}, 1);
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者,如果您想要X轴从横坐标 0 开始,但不显示任何具有负纵坐标的点,这可以通过添加例如来实现xmin=0, extra x ticks={0}

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\myConstFactor}{0.1*exp(-1/4)}% compute this only once
  \begin{axis}[
  xmin=0, extra x ticks={0},
  ymax=1.1, extra y ticks={1},
  restrict y to domain=0:1,
  width=8cm,
  height=6cm,
  axis lines = middle,
  xlabel={$T$}, ylabel={$\xi$},
  x label style={
    at={(current axis.right of origin)},
    anchor=west,
  },
  y label style={
    at={(current axis.above origin)},
    anchor=south,
  },
  style={thick},
  ]
  \addplot[blue, smooth, very thick, domain=0:7, samples=350]
    {1 - \myConstFactor*exp(1/x)};
  % Draw the asymptote for x → +∞
  \draw[red!80] (\pgfkeysvalueof{/pgfplots/xmin}, 1) --
                (\pgfkeysvalueof{/pgfplots/xmax}, 1);
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

按照@MS-SPO,只需0.15输入0

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{float}
\begin{document}
\begin{figure}[H]
%\captionof{figure}{Metodo }
\centering
\begin{tikzpicture}
  \begin{axis}[
  legend pos=outer north east,
  legend cell align={left},
  xmax=8,xmin=0,
  ymax=1,ymin=0,
  yticklabels=\empty, xticklabels=\empty,
  width=8cm,
  height=6cm,
  axis lines = middle,
  set layers,
  x label style={at={(1,0)},right},
  y label style={at={(0,1)},above},
  xlabel={$T$},ylabel={$\xi$},
  y tick style={draw=none}, x tick style={draw=none},
  style={thick}
               ]              
        \addplot [blue,smooth,very thick,domain=0.15:7,samples=200] 
        {1-( (0.1*exp(-1/4)) *exp(1/x))};
    \end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

答案3

基本与@frougon 几分钟前发布的答案相同,但我还是将其包括在内,因为我做了一些额外的更正/更改。

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=8cm, height=6cm,
xmin=0, xmax=8,
ymin=-2, ymax=1,
xtick=\empty, ytick=\empty,
axis lines=middle,
x label style={at={(xticklabel cs:1)}, below},
y label style={at={(yticklabel cs:1)}, above},
xlabel={$T$}, ylabel={$\xi$},
thick,
]              
\addplot [blue, very thick, domain=0:7, samples=100,  smooth, restrict y to domain=-10:1] {1-( (0.1*exp(-1/4)) *exp(1/x))};
\end{axis}
\end{tikzpicture}
\end{document}

图形

相关内容