同样的方法画双曲线函数,左边的由于定义域包含渐近线的零点而报错,意外给画了一条x=b,右边的定义域同样包含却正常编译,如何修改。
编译结果
\addplot[ % 函数f(x)=1/(x-1) + 1
domain = 1:7, % 定义域[1,7]
samples = 300, % 采样点密度(值越高曲线越平滑)
smooth, % 平滑曲线
% thick, % 粗线宽
line width = 0.5pt, % 设置为0.5磅线宽
blue % 颜色
] {-1/(x-7) + 1};
以上是左侧图像
\addplot[ % 函数f(x)=1/(x-1) + 1
domain = 1:7, % 定义域[1,7]
samples = 300, % 采样点密度(值越高曲线越平滑)
smooth, % 平滑曲线
% thick, % 粗线宽
line width = 0.5pt, % 设置为0.5磅线宽
blue % 颜色
] {1/(x-1) + 1};
以上是右侧图像
完整代码如下(同上文件)
\documentclass[tikz,border=5pt]{standalone}
\usepackage[UTF8]{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\usepackage{bm} % 或
\usepackage{amsmath}
\usepackage{silence}
\WarningFilter{latexfont}{} % 过滤所有字体警告
\pgfplotsset{compat=1.18} % 版本兼容性设置
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
node distance=1cm, % 节点间距
]
% 左侧图像节点
\node (left) {
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
%——xOy
xlabel = $x$,
ylabel = $y$,
x=0.5cm, % x轴每单位长度0.8cm
y=0.5cm, % y轴每单位长度0.8cm
width=11 * 0.5cm, % 根据x轴范围计算宽度
height=6 * 0.5cm, % 根据y轴范围计算高度
%将标签定位在x轴的最末端(刻度坐标系中,1表示100%位置)
xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
%
xmin = -1, xmax = 9,
ymin = -1, ymax = 5,
axis line style = {-{Triangle[scale=1]}}, % 修正箭头语法
%
%——刻度和刻度线
xtick = {1, 7}, %x标点
xticklabels = {$a$, $b$}, %x自定义
ytick = \empty, % 隐藏y轴刻度
tick style={draw=none}, % 隐藏所有刻度线
title={\large $\bm{f'(x) \leq 0}$},
title style={
at={(0.5,0)}, % 相对位置 (x,y) ∈ [0,1]
anchor=north, % 锚点方向
yshift=5pt % 垂直偏移
}
]
%图像
\node[below left] at (axis cs:0,0) {$O$};%原点
\addplot[ % 函数f(x)=1/(x-1) + 1
domain = 1:7, % 定义域[1,7]
samples = 300, % 采样点密度(值越高曲线越平滑)
smooth, % 平滑曲线
% thick, % 粗线宽
line width = 0.5pt, % 设置为0.5磅线宽
blue % 颜色
] {-1/(x-7) + 1}; % 函数表达式(deg()将弧度转角度)
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,5); % 从x=a处画垂直线
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,1/6+1); % 从x=b处画垂直线
% x=3 处切线
\addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{(1/16)*x + (17/16)};
% x=4 处切线
\addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{(1/9)*x + 8/9};
% 在右上角添加解析式
\node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt]
at (axis cs:3,4) % 定位
{\large $y=f(x)$};
\end{axis}
\end{tikzpicture}
};
% 右侧图像节点
\node[right=of left] (right) {
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
%——xOy
xlabel = $x$,
ylabel = $y$,
x=0.5cm, % x轴每单位长度0.8cm
y=0.5cm, % y轴每单位长度0.8cm
width=11 * 0.5cm, % 根据x轴范围计算宽度
height=6 * 0.5cm, % 根据y轴范围计算高度
%将标签定位在x轴的最末端(刻度坐标系中,1表示100%位置)
xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
%
xmin = -1, xmax = 9,
ymin = -1, ymax = 5,
axis line style = {-{Triangle[scale=1]}}, % 修正箭头语法
%
%——刻度和刻度线
xtick = {1, 7}, %x标点
xticklabels = {$a$, $b$}, %x自定义
ytick = \empty, % 隐藏y轴刻度
tick style={draw=none}, % 隐藏所有刻度线
title={$\bm{f'(x) \geq 0}$},
title style={
at={(0.5,0)}, % 相对位置 (x,y) ∈ [0,1]
anchor=north, % 锚点方向
yshift=5pt % 垂直偏移
}
]
%图像
\node[below left] at (axis cs:0,0) {$O$};%原点
\addplot[ % 函数f(x)=1/(x-1) + 1
domain = 1:7, % 定义域[1,7]
samples = 300, % 采样点密度(值越高曲线越平滑)
smooth, % 平滑曲线
% thick, % 粗线宽
line width = 0.5pt, % 设置为0.5磅线宽
blue % 颜色
] {1/(x-1) + 1}; % 函数表达式(deg()将弧度转角度)
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,5); % 从x=a处画垂直线
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,1/6+1); % 从x=b处画垂直线
% x=3 处切线
\addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{-0.25*x + 2.25};
% x=4 处切线
\addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{-1/9*x + 16/9};
% 在右上角添加解析式
\node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt]
at (axis cs:3,4) % 定位
{\large $y=f(x)$};
\end{axis}
\end{tikzpicture}
};
% 在整体图形下方添加标题节点
\node[below=10pt of current bounding box.south,
anchor=north]{图 \\ 3.3};
\title{123}
\end{tikzpicture}
\end{document}
首先,不建议使用
「同一个node里」左右放置两个tikzpicture
,比较难管理
更建议「分别」建立「A.tex」绘制左图和「B.tex」绘制右图,再用传统插入图片PDF与caption的方式排版主文档,这样还有一个好处,caption可以使用超链接,也不用手动编号.
本着分而治之的思想,在找问题时最好将源代码分为不出问题的和出问题的两份。
1 / (x-1) + 1
」:\documentclass[tikz,border=5pt]{standalone}
\usepackage{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\pgfplotsset{compat=1.18}
\usepackage{bm} % 或
\usepackage{amsmath,amsfonts}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xlabel = $x$,
ylabel = $y$,
x=0.5cm, % x轴每单位长度0.8cm
y=0.5cm, % y轴每单位长度0.8cm
width=11 * 0.5cm, % 根据x轴范围计算宽度
height=6 * 0.5cm, % 根据y轴范围计算高度
xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
xmin = -1, xmax = 9,
ymin = -1, ymax = 5,
axis line style = {-{Triangle[scale=1]}}, % 修正箭头语法
%——刻度和刻度线
xtick = {1, 7}, %x标点
xticklabels = {$a$, $b$}, %x自定义
ytick = \empty, % 隐藏y轴刻度
tick style={draw=none}, % 隐藏所有刻度线
title={$\bm{f'(x) \geq 0}$},
title style={
at={(0.5,0)}, % 相对位置 (x,y) ∈ [0,1]
anchor=north, % 锚点方向
yshift=5pt % 垂直偏移
}
]
\node[below left] at (axis cs:0,0) {$O$};%原点
\addplot[ % 函数f(x)=1/(x-1) + 1
domain = 1:7, % 定义域[1,7]
samples = 300, % 采样点密度(值越高曲线越平滑)
smooth, % 平滑曲线
line width = 0.5pt, % 设置为0.5磅线宽
blue % 颜色
] { 1 / (x-1) + 1 }; % 函数表达式(deg()将弧度转角度)
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,5); % 从x=a处画垂直线
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,1/6+1); % 从x=b处画垂直线
% x=3 处切线
\addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{-0.25*x + 2.25};
% x=4 处切线
\addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{-1/9*x + 16/9};
% 在右上角添加解析式
\node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt]
at (axis cs:3,4) {\large $y=f(x)$};
\end{axis}
\end{tikzpicture}
\end{document}
-1 / ( x-7 ) + 1
」\documentclass[tikz,border=5pt]{standalone}
\usepackage{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\usepackage{bm} % 或
\pgfplotsset{compat=1.18}
\usepackage{amsmath,amsfonts}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
%——xOy
xlabel = $x$,
ylabel = $y$,
x=0.5cm, % x轴每单位长度0.8cm
y=0.5cm, % y轴每单位长度0.8cm
width=11 * 0.5cm, % 根据x轴范围计算宽度
height=6 * 0.5cm, % 根据y轴范围计算高度
%将标签定位在x轴的最末端(刻度坐标系中,1表示100%位置)
xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
%
xmin = -1, xmax = 9,
ymin = -1, ymax = 5,
axis line style = {-{Triangle[scale=1]}}, % 修正箭头语法
%
%——刻度和刻度线
xtick = {1, 7}, %x标点
xticklabels = {$a$, $b$}, %x自定义
ytick = \empty, % 隐藏y轴刻度
tick style={draw=none}, % 隐藏所有刻度线
title={\large $\bm{f'(x) \leq 0}$},
title style={
at={(0.5,0)}, % 相对位置 (x,y) ∈ [0,1]
anchor=north, % 锚点方向
yshift=5pt % 垂直偏移
}
]
%图像
\node[below left] at (axis cs:0,0) {$O$};%原点
\addplot[ % 函数f(x)=1/(x-1) + 1
domain = 1:7, % 定义域[1,7]
samples = 300, % 采样点密度(值越高曲线越平滑)
smooth, % 平滑曲线
% thick, % 粗线宽
line width = 0.5pt, % 设置为0.5磅线宽
blue % 颜色
] { -1 / ( x - 7 ) + 1 };
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,5);% 从x=a处画垂直线
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,1/6+1); % 从x=b处画垂直线
% x=3 处切线
\addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{(1/16)*x + (17/16)};
% x=4 处切线
\addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{(1/9)*x + 8/9};
% 在右上角添加解析式
\node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt]
at (axis cs:3,4) % 定位
{\large $y=f(x)$};
\end{axis}
\end{tikzpicture}
\end{document}
这将报错并且得不到「有效的PDF文件」,有错误必须纠正,不可以忽略。
! Dimension too large.
<recently read> \pgf@yy
l.60 \end{axis}
其实维数太大意味着是正无穷呗~ 观察下面这段:
\addplot[ % 函数f(x)=1/(x-1) + 1
domain = 1:7, % 定义域[1,7]
samples = 300, % 采样点密度(值越高曲线越平滑)
smooth, % 平滑曲线
% thick, % 粗线宽
line width = 0.5pt, % 设置为0.5磅线宽
blue % 颜色
] { -1 / ( x - 7 ) + 1 };
当domain
中的x=7
不就除零了...
我不知道你的最终目的是什么,但是改成domain = 1:6.95
即可:
\documentclass[tikz,border=5pt]{standalone}
\usepackage{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\usepackage{bm} % 或
\pgfplotsset{compat=1.18}
\usepackage{amsmath,amsfonts}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
%——xOy
xlabel = $x$,
ylabel = $y$,
x=0.5cm, % x轴每单位长度0.8cm
y=0.5cm, % y轴每单位长度0.8cm
width=11 * 0.5cm, % 根据x轴范围计算宽度
height=6 * 0.5cm, % 根据y轴范围计算高度
%将标签定位在x轴的最末端(刻度坐标系中,1表示100%位置)
xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
%
xmin = -1, xmax = 9,
ymin = -1, ymax = 5,
axis line style = {-{Triangle[scale=1]}}, % 修正箭头语法
%
%——刻度和刻度线
xtick = {1, 7}, %x标点
xticklabels = {$a$, $b$}, %x自定义
ytick = \empty, % 隐藏y轴刻度
tick style={draw=none}, % 隐藏所有刻度线
title={\large $\bm{f'(x) \leq 0}$},
title style={
at={(0.5,0)}, % 相对位置 (x,y) ∈ [0,1]
anchor=north, % 锚点方向
yshift=5pt % 垂直偏移
}
]
%图像
\node[below left] at (axis cs:0,0) {$O$};%原点
\addplot[ % 函数f(x)=1/(x-1) + 1
domain = 1:6.95, % 定义域[1,7]
samples = 300, % 采样点密度(值越高曲线越平滑)
smooth, % 平滑曲线
% thick, % 粗线宽
line width = 0.5pt, % 设置为0.5磅线宽
blue % 颜色
] { -1 / ( x - 7 ) + 1 };
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:7,0) -- (axis cs:7,5);% 从x=a处画垂直线
\draw[dash pattern=on 1pt off 1pt, lightgray] (axis cs:1,0) -- (axis cs:1,1/6+1); % 从x=b处画垂直线
% x=3 处切线
\addplot[domain=0.7:7.3, smooth, red, line width= 0.5pt]{(1/16)*x + (17/16)};
% x=4 处切线
\addplot[domain=0.7:7.3, smooth, red, line width=0.5pt]{(1/9)*x + 8/9};
% 在右上角添加解析式
\node[anchor=north west, align=left, xshift=-5pt, yshift=-10pt]
at (axis cs:3,4) % 定位
{\large $y=f(x)$};
\end{axis}
\end{tikzpicture}
\end{document}
至于为什么:
意外给画了一条x=b
因为当x=7
时,除零了没法计算,计算得到的y
值遍历了(-oo,+oo)
,也就是维数太多,画成图就是一条x=b
的直线,而且报错。