我想用顶部绘制二次函数的对称轴。问题是轴的定位不太好。将 xmin=-6 改为 xmin=-2 会使红线位于错误的位置,而且顶部也不在正确的位置。因此,我想用函数最大值或最小值的 x 坐标定义红线。这样改变 f(x) 就可以调整绘图。
我能想到的最好的办法是这样的:
\documentclass[symmetric,justified,a4paper,sfsidenotes]{tufte-book} % use larger type; default would be 10pt
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{pgf,tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{calc,intersections,through,backgrounds,snakes}
\usepackage{pgfplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.5]
\begin{axis}%
[
anchor=center,
grid=major,
x=1cm, y=1cm,
minor x tick num=1,
xmin=-6, xmax=6,
xlabel={\scriptsize $x$},
axis x line=middle,
minor y tick num=1,
ymin=-6, ymax=6,
ylabel={\scriptsize $f(x)$},
tick label style={font=\tiny},
axis y line=middle,
no markers,
samples=100,
domain=-6:6,
restrict y to domain=-20:20,
]
\addplot[black, thick] (x,{2(x-2)^2-3});
\end{axis}
\draw[color=red, thick] (2,-6) -- (2,6) node[right] {\scriptsize $x=2$};
\coordinate (T) at (2,-3) ;
\tkzDrawPoints(T);
\node[right] at (T) {$T(p,q)$};
\end{tikzpicture}
\end{center}
\end{document}
答案1
这是一种自动寻找最小值的方法(也适用于最大值):
代码:
\documentclass[symmetric,justified,a4paper,sfsidenotes]{tufte-book} % use larger type; default would be 10pt
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{pgf,tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{calc,intersections,through,backgrounds,snakes}
\usetkzobj{all} %om hoeken te markeren?
\usepackage{pgfplots}
\makeatletter
\pgfplotsset{
compat=1.12,
/tikz/max node/.style={
anchor=south,
},
/tikz/min node/.style={
anchor=north,
name=minimum
},
mark min/.style={
point meta rel=per plot,
visualization depends on={x \as \xvalue},
scatter/@pre marker code/.code={%
\ifx\pgfplotspointmeta\pgfplots@metamin
\def\markopts{}%
\coordinate (minimum);
\xdef\XMinimum{\xvalue},%
\xdef\YMinimum{\pgfplotspointmeta},%
\else
\def\markopts{mark=none}
\fi
\expandafter\scope\expandafter[\markopts,every node near coord/.style=green]
},%
scatter/@post marker code/.code={%
\endscope
},
scatter,
},
mark max/.style={
point meta rel=per plot,
visualization depends on={x \as \xvalue},
scatter/@pre marker code/.code={%
\ifx\pgfplotspointmeta\pgfplots@metamax
\def\markopts{}%
\coordinate (maximum);
\xdef\XMaximum{\xvalue},%
\xdef\YMaximum{\pgfplotspointmeta},%
\else
\def\markopts{mark=none}
\fi
\expandafter\scope\expandafter[\markopts]
},%
scatter/@post marker code/.code={%
\endscope
},
scatter
}
}
\makeatother
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
[
anchor=center,
grid=major,
minor x tick num=1,
xmin=-6, xmax=6,
xlabel={\scriptsize $x$},
axis x line=middle,
minor y tick num=1,
ymin=-6, ymax=6,
ylabel={\scriptsize $f(x)$},
tick label style={font=\tiny},
axis y line=middle,
no markers,
samples=100,
domain=-6:6,
after end axis/.code={
\draw[red,thick] (minimum|-{axis cs:0,-6}) --(minimum|-{axis cs:0,6});
\tkzDrawPoints(minimum);
\node[below right] at (minimum) {$T(p,q)$};
\node[below right,red] at (minimum|-{axis cs:0,6}) {\scriptsize $x=\pgfmathprintnumber[fixed]{\XMinimum}$};
}
]
\addplot[black, thick,mark min] {2*(x-2)^2-3};
\end{axis}
\coordinate (T) at (2,-3) ;
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
[
anchor=center,
grid=major,
minor x tick num=1,
xmin=-4, xmax=4,
ymax=70,ymin=-30,
xlabel={\scriptsize $x$},
axis x line=middle,
minor y tick num=1,
ylabel={\scriptsize $f(x)$},
tick label style={font=\tiny},
axis y line=middle,
no markers,
samples=100,
domain=-5:5,
after end axis/.code={
\draw[red,thick] (minimum) -- (minimum|-{axis cs:0,70});
\draw[red,thick] (minimum-|{axis cs:-4,0}) -- (minimum-|{axis cs:4,0});
\tkzDrawPoints(minimum);
\node[below right] at (minimum) {$T(p,q)$};
\node[below right,red] at (minimum|-{axis cs:0,70}) {\scriptsize $x=\pgfmathprintnumber[fixed]{\XMinimum}$};
\node[below right,red] at (minimum-|{axis cs:-4,0}) {\scriptsize $y=\pgfmathprintnumber[fixed]{\YMinimum}$};
}
]
\addplot[black,thick,mark min] {(0.73*x-1)*(x-3)*(x+2)*(x+3)};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
我使用了 Jake 代码的一个变体his answer
到如何使用 pgfplots 和 scatter 自动标记局部极值?
答案2
在环境 内绘制线会更容易axis
,因为坐标隐式地使用坐标系axis cs
。\tkzDrawPoints
这种方式会造成麻烦,因为我得到了未定义的\point
错误。因此,示例以相同的方式设置点,\tkzDrawPoints
可以通过具有 样式 的节点来完成point style
:
\documentclass[symmetric,justified,a4paper,sfsidenotes]{tufte-book}
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{pgf,tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{calc,intersections,through,backgrounds,snakes}
\usetkzobj{all} %om hoeken te markeren?
\usepackage{pgfplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.5]
\begin{axis}%
[
anchor=center,
grid=major,
x=1cm, y=1cm,
minor x tick num=1,
xmin=-2, xmax=6,
xlabel={\scriptsize $x$},
axis x line=middle,
minor y tick num=1,
ymin=-6, ymax=6,
ylabel={\scriptsize $f(x)$},
tick label style={font=\tiny},
axis y line=middle,
no markers,
samples=100,
domain=-6:6,
restrict y to domain=-20:20,
]
\addplot[black, thick] (x,{2(x-2)^2-3});
\draw[color=red, thick] (axis cs:2,-6) -- (axis cs:2,6)
node[below right] {\scriptsize $x=2$};
\coordinate (T) at (axis cs:2,-3);
\node[point style] at (T) {};
\node[below right] at (T) {$T(p,q)$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}