我有一个使用 pgfplots 绘制的函数:
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{helvet}
\usepackage{sansmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$X$,
ylabel=$Y$,
axis x line=center,
axis y line=center,
grid,
grid style={green},
major tick style={draw=black, thick},
xticklabels={,,},
yticklabels={,,},
xmin=-5,
xmax=5,
ymin=-5,
ymax=5,
xtick={-5,-4,...,5},
ytick={-5,-4,...,5},
axis line style={thick},
samples=250,
unit vector ratio*=1 1,
tick label style = {font=\sansmath\sffamily},
every axis label = {font=\sansmath\sffamily},
legend style = {font=\sansmath\sffamily},
label style = {font=\sansmath\sffamily},
]
\addplot [ultra thick, red]{42/27 *((x*x*x)/3 - (x*x)/2 - 2*x) +59/27} node [pos=0.75,left,black,{font=\sansmath\sffamily}] {$f(x)$};
\draw[very thick, blue] (axis cs: -2.5,4)--(axis cs: 0.5,4);
\draw[very thick, blue] (axis cs: 3.5,-3)--(axis cs: 0.5,-3);
\node[above] at (axis cs: -1,4) {\sffamily\textit P};
\node[below] at (axis cs: 2,-3) {\sffamily\textit Q};
\end{axis};
\end{tikzpicture}
\end{document}
该代码生成的图形是第一个。第二个才是我想要实现的。
然后,我想做一些在两个图形中都能看到的改变:
- 放大轴以将“X”和“Y”置于网格之外,没有
x = −6
在和处打勾y = −6
。(我的意思是,我想获得第二个图形的轴,但不想通过“ ”完成左侧部分的 X 轴|---
,而是通过“---
”完成…… Y 轴也是一样)。 x = 1
仅在和 中放置标签y = 1
。- (如果可能的话)删除
font=\sansmath\sffamily
节点f(x)
并将其包含在轴选项中以影响所有节点。 - (如果可能的话)。除了像我一样在 TikZ 中手动执行之外,还有其他方法可以指示 P 或 Q 等点中的切线吗?
答案1
您可以使用它
axis line style={shorten > = -0.5cm, shorten < = -0.5cm}
来延长轴线。请注意,这不会增加边界框,因此您可能需要手动调整。您可以使用它
extra x ticks={1}, extra x tick label=\pgfmathprintnumber{\tick}
来打印刻度标签。- 您可以
font=\sansmath\sffamily
输入axis
选项以将其应用于轴上的所有文本。 - 您可能应该就此提出一个单独的问题。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{helvet}
\usepackage{sansmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$X$,
ylabel=$Y$,
axis x line=center, xlabel style={anchor=south west},
axis y line=center, ylabel style={anchor=south west},
grid,
grid style={green},
major tick style={draw=black, thick},
xmin=-5,
xmax=5,
ymin=-5,
ymax=5,
xtick={-5,-4,...,5},
xticklabels={},
extra x ticks={1},
extra x tick label=\pgfmathprintnumber{\tick},
ytick={-5,-4,...,5},
yticklabels={},
extra y ticks={1},
extra y tick labels=1,
axis line style={thick, shorten > = -0.5cm, shorten < = -0.5cm},
samples=50,
unit vector ratio*=1 1,
font=\sansmath\sffamily,
]
\addplot [ultra thick, red, smooth]{42/27 *((x*x*x)/3 - (x*x)/2 - 2*x) +59/27} node [pos=0.75,left,black] {$f(x)$};
\draw[very thick, blue] (axis cs: -2.5,4)--(axis cs: 0.5,4);
\draw[very thick, blue] (axis cs: 3.5,-3)--(axis cs: 0.5,-3);
\node[above] at (axis cs: -1,4) {$P$};
\node[below] at (axis cs: 2,-3) {$Q$};
\end{axis};
\path (current axis.south west) +(-0.5cm,-0.5cm) (current axis.north east) +(0.5cm,0.5cm);
\end{tikzpicture}
\end{document}