我构建了上述函数的曲线,但是显示不正确。
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel=$y$,
domain=0:0.6,
samples=100,
axis lines=middle,
xmin=0, xmax=0.6,
ymin=-0.4, ymax=0.2,
enlargelimits=true,
xtick={0,0.1,0.2,0.3,0.4,0.5,0.6},
ytick={-0.1,0,0.1,0.2},
grid=both,
width=10cm,
height=7cm,
]
\addplot[blue, ultra thick] {x*e^(1/x) - e^((1-x^2)/x) - x - 1};
\addlegendentry{\( (\mathscr{C}_h) \)}
\end{axis}
\end{tikzpicture}
\end{document}
这就是我想要的
答案1
如果你在类似的域上绘制它,你会得到类似的图:
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel=$y$,
domain=0.55:4,
samples=100,
axis lines=middle,
% xmin=0, xmax=4,
% ymin=-0.4, ymax=0.2,
enlargelimits=true,
% xtick={0,0.1,0.2,0.3,0.4,0.5,0.6},
% ytick={-0.1,0,0.1,0.2},
grid=both,
width=10cm,
height=7cm,
]
\addplot[blue, ultra thick] {x*e^(1/x) - e^((1-x^2)/x) - x - 1};
% \addlegendentry{\( (\mathscr{C}_h) \)}
\end{axis}
\end{tikzpicture}
\end{document}
(我会避免手动设置最小值/最大值和刻度,pgfplots 通常在这方面做得很好)
答案2
即使不知道设置 的最佳值domain
,你也可以使用 来保护自己免受过大数字和差异的影响restrict y to domain
。例如:
restrict y to domain=-10:10,
用替换下方的所有y
值,用替换上方的所有值。还有一种带星号的形式,它只是截断为 [ , ](还有 一个键 和一个键)。-10
-inf
y
10
+inf
-10
10
restrict x to domain
restrict z to domain
完整示例:
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel=$y$,
domain=0:10,
samples=100,
axis lines=middle,
xlabel style={below},
xmin=0, xmax=4,
ymin=-2, ymax=0.6,
restrict y to domain=-10:10,
enlargelimits=true,
% xtick={0,0.1,0.2,0.3,0.4,0.5,0.6},
% ytick={-0.1,0,0.1,0.2},
grid=both,
width=10cm,
height=7cm,
]
\addplot[blue, ultra thick] {x*e^(1/x) - e^((1-x^2)/x) - x - 1};
% \addlegendentry{\( (\mathscr{C}_h) \)}
\end{axis}
\end{tikzpicture}
\end{document}