我有一个分段函数图。定义域从 到-2
,2
范围从-8
到8
。我指定width=2.5in
和height=2.5in
和axis equal
。这些指定不应该给我沿轴的相同缩放比例吗?(“如果x
图上向右移动每厘米对应于 的变化1
,则图上向上移动每x
厘米对应于 的变化1
。”)为什么x
- 轴是从 到 绘制-8
的8
?
\documentclass[10pt]{amsart}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=2.5in, height=2.5in, axis equal, axis on top, clip=false,
axis lines=middle,
xmin=-2,xmax=2, domain=-2:2,
ymin=-8,ymax=8,
restrict y to domain=-8:8,
xtick={\empty},ytick={\empty},
axis lines=middle,
axis line style={latex-latex},
xlabel=\textit{x},ylabel=\textit{y},
axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
xlabel style={at={(ticklabel* cs:1)}, xshift=7.5pt, anchor=north west},
ylabel style={at={(ticklabel* cs:1)}, yshift=7.5pt, anchor=south west}
]
\addplot[samples=501, domain=-2:0] {-x^3};
\addplot[samples=2, domain=0:1] {x + 1};
\addplot[samples=2, domain=1:2] {-7} node[anchor=west, pos=1, font=\scriptsize]{\makebox[0pt][l]{$y=f(x)$}};
\coordinate (P) at (0,0);
\coordinate (Q) at (0,1);
\coordinate (R) at (1,-7);
\end{axis}
\draw[fill=white] (P) circle (1.5pt);
\draw[fill] (Q) circle (1.5pt);
\draw[fill=white] (R) circle (1.5pt);
\end{tikzpicture}
\end{document}
答案1
确实,axis equal
保证两个轴的缩放比例相同,但它也常常会产生不理想的效果,即扩大它们的限制(第 4.10 节pgfplots 文档)。
如果您想x
从-2
到2
,那么这axis equal image
就是要走的路。此选项将使单位相同,但将变量保持在您指定的值内。
代码如下:
\documentclass[10pt]{amsart}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=2.5in,
axis equal image,
axis on top,
clip=false,
axis lines=middle,
xmin=-2,
xmax=2,
domain=-2:2,
ymin=-8,
ymax=8,
restrict y to domain=-8:8,
xtick={\empty},
ytick={\empty},
axis line style={latex-latex},
xlabel=\textit{x},
ylabel=\textit{y},
axis line style={
shorten >=-7.5pt,
shorten <=-7.5pt
},
xlabel style={
at={(ticklabel* cs:1)},
xshift=7.5pt,
anchor=north west
},
ylabel style={
at={(ticklabel* cs:1)},
yshift=7.5pt,
anchor=south west
}
]
\addplot[samples=501, domain=-2:0] {-x^3};
\addplot[samples=2, domain=0:1] {x + 1};
\addplot[samples=2, domain=1:2] {-7} node[anchor=west, pos=1, font=\scriptsize]{\makebox[0pt][l]{$y=f(x)$}};
\coordinate (P) at (0,0);
\coordinate (Q) at (0,1);
\coordinate (R) at (1,-7);
\end{axis}
\draw[fill=white] (P) circle (1.5pt);
\draw[fill] (Q) circle (1.5pt);
\draw[fill=white] (R) circle (1.5pt);
\end{tikzpicture}
\end{document}
以下是最终的剧情:
请注意,我们可以去掉 或height
的width
规范。一旦我们设置了 (例如,英寸height
),width
就会立即受到限制,因此x
会从-2
到2
以与垂直轴相同的步长变化。