我想为我的学生重新创建我在书上找到的一张图。该图有一个网格(1 毫米 x 1 毫米),上方是轴和函数图。
我在创建网格时遇到了麻烦,网格每隔 5 毫米会有一些强调。我认为我必须使用minor grid style
和major grid style
设置网格的颜色及其强调。
那么,我该如何设置网格,使其具有 1 毫米乘 1 毫米的划分?我想指出的是,设置“刻度”会在轴上产生小黑线,而这并不是我想要的,我只想得到网格。
这是我的 MWE:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{tikz,tikz-3dplot} %Para fazer desenhos
\usetikzlibrary{shapes.multipart,shapes.geometric,calc,angles,positioning,intersections,quotes,decorations,babel,patterns,fit}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
axis lines = {center},
%width = {0.6\linewidth},
ylabel = {$y$},
xlabel = {$x$},
ytick distance = {2},
xtick distance = 1,
ymin=-1,
ymax=11,
xmin=-0.9,
xmax=4.9,
major grid style={gray},
minor grid style={lightgray},
grid=both
]
\addplot [
mark = none, domain= -1:5, smooth %samples at = {-1,0,1,2,3,4,5}
]
{x^2 -5*x + 6};
\addplot [
mark=*
]
coordinates {(2.5,-0.25)};
\end{axis}
\end{tikzpicture}
\end{document}
这是我试图以此为基础的图像。我知道 MWE 中的函数与图像不同,但它只是网格的参考。我的想法是提供一些函数示例以及一些仅具有轴和网格的实例,以便学生可以构建图表:
答案1
作为纯tikz
图片:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
% grid
\draw[ thin,brown!25] (-2,-2) grid[step= 1mm] ++ (8,15);
\draw[semithick,brown!50] (-2,-2) grid[step= 5mm] ++ (8,15);
\draw[ thick,brown!75] (-2,-2) grid[step=10mm] ++ (8,15);
% axis
\draw[-Stealth] (0,-1) -- (0,11) node[below right] {$y$};
\draw[-Stealth] (-1,0) -- (5, 0) node[below left] {$x$};
\foreach \i in {1,...,4}{\draw (\i,1mm) -- + (0,-2mm) node[below] {\i};}
\foreach \i in {2,4,...,10}{\draw (1mm,\i) -- + (-2mm,0) node[ left] {\i};}
% curve
\draw[very thick, red] plot[domain= -1:5] (\x, \x*\x - 5*\x + 6);
\draw plot[mark=*] coordinates {(2.5,-0.25)};
\end{tikzpicture}
\end{document}
答案2
这里有一个答案pgfplots
,有三个网格级别并且删除了刻度:
为了获得合适的毫米网格,您必须将 x 和 y 向量设置为固定值,从而禁用自动缩放 ( )。这里主网格需要 1cm。通过使用和x=1cm,y=1cm
定义 2 个主刻度之间的刻度数来设置次网格。minor x tick num=9
minor y tick num=9
pgfplots
仅提供两个网格级别,但可以手动绘制第三个。这需要启用图层才能在轴下方绘制中间网格(使用 完成\pgfplotsset{set layers=standard}
)。\draw
然后必须输入该命令\begin{pgfonlayer}{axis grid} ... \end{pgfonlayer}
。为了使中间网格适用于没有绘图的方格纸,\foreach
使用了两个循环。
如果您仅想要方格纸,则只需省去图表(即没有\addplot
)。
可以使用 去除蜱虫\pgfplotsset{every major tick/.style={draw=none}, every minor tick/.style={draw=none}}
。
我还可以自由地将斧头弄得更粗一些。
结果如下:
代码:
\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
%\usepackage{tikz,tikz-3dplot} %Para fazer desenhos
\usetikzlibrary{shapes.multipart,shapes.geometric,calc,angles,positioning,intersections,quotes,decorations,babel,patterns,fit}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}[%
% style for middle grid
middle grid style/.style={lightgray,line width=0.5pt}
]
\pgfplotsset{%
% enable layer, needed to draw middle grid below axis
set layers=standard,
% disable ticks
every major tick/.style={draw=none},
every minor tick/.style={draw=none},
}
\begin{axis} [
axis lines = {center},
% set fixed scale to get mm grid
% note: this is for the major grid
x=1cm,
y=1cm,
ylabel = {$y$},
xlabel = {$x$},
ytick distance = 1,
xtick distance = 1,
% number of minor ticks between 2 major ticks
minor x tick num=9,
minor y tick num=9,
ymin=-1,
ymax=11,
xmin=-0.9,
xmax=4.9,
major grid style={lightgray,thick},
minor grid style={lightgray,very thin},
grid=both,
% optional stuff
% change axis line width
axis line style={thick},
% make tick labels cover the grid
%ticklabel style={inner sep=1pt,fill=white},
]
% draw middle grid
\begin{pgfonlayer}{axis grid}
% works only for x=y=1cm with plots
%\draw[middle grid style,step=0.5cm]
% (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin}) grid
% (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\pgfkeysvalueof{/pgfplots/ymax});
% to be used for graph paper with no plots (and if x != y)
% v-- first, second and last x-positions for middle grid
\foreach \x in {-0.5,0.5,...,4.5}{
% \edef-trick, see manual page 541
\edef\temp{\noexpand\draw[middle grid style]
(axis cs:\x,\pgfkeysvalueof{/pgfplots/ymin}) --
(axis cs:\x,\pgfkeysvalueof{/pgfplots/ymax});}
\temp
}
% v-- first, second and last y-positions for middle grid
\foreach \y in {-0.5,0.5,...,10.5}{
\edef\temp{\noexpand\draw[middle grid style]
(axis cs:\pgfkeysvalueof{/pgfplots/xmin},\y) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\y);}
\temp
}
\end{pgfonlayer}
\addplot [
mark = none, domain= -1:5, smooth %samples at = {-1,0,1,2,3,4,5}
]
{x^2 -5*x + 6};
\addplot [
mark=*
]
coordinates {(2.5,-0.25)};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
您缺少一些axis
环境参数:
\begin{axis}[
axis lines = {center},
%width = {0.6\linewidth},
ylabel = {$y$},
xlabel = {$x$},
ytick distance = 2,
xtick distance = 1,
ymin=-1,
ymax=11,
xmin=-0.9,
xmax=4.9,
grid=both,
% here's what I changed/added:
grid style={line width=.1pt,draw=lightgray},
major grid style={line width=.2pt,draw=gray},
minor tick num=4,
enlargelimits={abs=0.5},
tick style={draw=none},
axis line style={latex-latex},
ticklabel style={font=\tiny,fill=white},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
现在结果已经足够接近你的书了:
请注意,轴上的单位大小不同;这是由于刻度距离造成的。
答案4
使用 tikz 及其 plot 命令的答案(我发现它比 pgfplot 更灵活addplot
:)请小心使用 lastscale
来更改函数。您也可以使用yscale
或xscale
。在 plot 命令中定义颜色的名称也可能有用。
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
Grid (with grid command):
\noindent\begin{tikzpicture}
\draw[step=1mm,color=orange,very thin] (-5 cm,-3cm) grid (5cm,3cm);
\end{tikzpicture}
Custom Grid (with foreach command):
\xdef\xmin{-5}
\xdef\xsec{-4.9}
\xdef\xmax{5.01}
\xdef\ymin{-3}
\xdef\ysec{-2.9}
\xdef\ymax{3.01}
\noindent\begin{tikzpicture}
\foreach \x in {\xmin,\xsec,...,\xmax} {
\draw [thin,orange] (\x,\ymin) -- (\x,\ymax);
}
\foreach \y in {\ymin,\ysec,...,\ymax} {
\draw [very thin,orange] (\xmin,\y) -- (\xmax,\y);
}
\end{tikzpicture}
\pagebreak
Grid+Axes (with grid command):
\noindent\begin{tikzpicture}
\draw[step=1mm,color=orange,very thin] (-5 cm,-3cm) grid (5cm,3cm);
\draw[-latex] (-4.5,0)--(4.5,0) node [below] {$x$};
\draw[-latex] (0,-2.5)--(0,2.5) node[left] {$y$};
\end{tikzpicture}
Custom Grid+Axes (with foreach command):
\xdef\xmin{-5}
\xdef\xsec{-4.9}
\xdef\xmax{5.01}
\xdef\ymin{-3}
\xdef\ysec{-2.9}
\xdef\ymax{3.01}
\noindent\begin{tikzpicture}
\foreach \x in {\xmin,\xsec,...,\xmax} {
\draw [thin,orange] (\x,\ymin) -- (\x,\ymax);
}
\foreach \y in {\ymin,\ysec,...,\ymax} {
\draw [very thin,orange] (\xmin,\y) -- (\xmax,\y);
}
\draw[-latex] (-4.5,0)--(4.5,0) node [below] {$x$};
\draw[-latex] (0,-2.5)--(0,2.5) node[left] {$y$};
\end{tikzpicture}
\pagebreak
Grid+Axes+Plot (with grid command):
\noindent\begin{tikzpicture}
\draw[step=1mm,color=orange,very thin] (-5 cm,-3cm) grid (5cm,3cm);
\draw[-latex] (-4.5,0)--(4.5,0) node [below] {$x$};
\draw[-latex] (0,-2.5)--(0,2.5) node[left] {$y$};
\draw[domain=-2.1:2.1,variable=\x] plot ({\x},{\x*\x-2}); %y=x^2-2
\end{tikzpicture}
Custom Grid+Axes+Plot (with foreach command):
\xdef\xmin{-5}
\xdef\xsec{-4.9}
\xdef\xmax{5.01}
\xdef\ymin{-3}
\xdef\ysec{-2.9}
\xdef\ymax{3.01}
\noindent\begin{tikzpicture}
\foreach \x in {\xmin,\xsec,...,\xmax} {
\draw [thin,orange] (\x,\ymin) -- (\x,\ymax);
}
\foreach \y in {\ymin,\ysec,...,\ymax} {
\draw [very thin,orange] (\xmin,\y) -- (\xmax,\y);
}
\draw[-latex] (-4.5,0)--(4.5,0) node [below] {$x$};
\draw[-latex] (0,-2.5)--(0,2.5) node[left] {$y$};
\draw[domain=-1:4,variable=\x,scale=0.5] plot ({\x},{\x*\x-3*\x+1}); %y=x^2-3x+1
\end{tikzpicture}
\end{document}
输出: