网格线与轴的坐标不一致,点的坐标也不一致。如何解决这个问题?
\documentclass[dutch,10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amssymb}
\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=2.00cm]{geometry}
\usepackage{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{decorations.shapes}
\begin{document}
\tikzset{decorate sep/.style 2 args=
{decorate,decoration={shape backgrounds,shape=circle,shape size=#1,shape sep=#2}}}
\begin{tikzpicture}
\begin{axis}[width=15cm, axis lines=middle,
xmin=-1,xmax=19,
ymin=-1,ymax=16,
xtick={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18},ytick=
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
xticklabels={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18},yticklabels=
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
ticklabel style={},
axis line style={very thick, latex-latex},
axis line style={very thick, ->},
x label style={at={(ticklabel* cs:1)},
anchor=west,},
y label style={at={(ticklabel* cs:1)},
anchor=south},
xlabel={$x$},
ylabel={$y$},
unit vector ratio*=1 1 1]
\draw[step=1cm,black,very thin] (axis cs:0,0) grid (axis cs:5,5);
\draw[decorate sep={2mm}{5mm},fill] (axis cs:0,1) -- (axis cs: 0,4);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您正在使用
step = 1cm
其步长为 1 厘米。
你想使用
step = {(axis direction cs:1,1)}
以便 TikZ 根据轴坐标系使用适当的步长。
我提供pgfplots step = <x> by <y>
哪些用途step = {(axis direction cs:<x>,<y>)}
以便于输入。
对于沿路径放置形状,也会发生类似的事情。5mm
每个刻度之间的距离根本不正确。此外,装饰还会区分放置在中心和边界之间给定距离的形状。
在这里,我将提供pgfplots y shape sep
一个是方向并确定所需的shape sep
,但也适用between centers
。
但您也可以只使用循环(或者可能是事先进行更多工作的链):
\pgfplotsinvokeforeach{1,...,4}{
\node[shape=circle, inner sep=+0pt, minimum size=2mm, draw, fill] at (0,#1) {};
}
对于网格(其内部只不过是两个循环)也是如此,但使用grid
带有方向坐标的键应该会有所帮助……除非 PGFPlots 坐标系的原点不在 PGF/TikZ 图片的原点所在位置。(我不知道这是否真的如此。)
我在用着
\pgfplotsset{compat = 1.18}
所以我们可以(0, 1)
直接使用而不必使用axis cs:
。
代码
\documentclass[tikz]{standalone}
%\documentclass[dutch,10pt,a4paper]{article}
%\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=2.00cm]{geometry}
%\usepackage{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{decorations.shapes}
\makeatletter
\tikzset{
pgfplots step/.style args={#1 by #2}{step={(axis direction cs:#1,#2)}},
/pgf/decoration/pgfplots y shape sep/.code=%
\pgf@process{\pgfplotspointaxisdirectionxy{0}{#1}}%
\pgfkeysalso{/pgf/decoration/shape sep/.expanded={\the\pgf@y,between centers}}%
}
\makeatother
\begin{document}
\begin{tikzpicture}[
decorate sep/.style 2 args={
decorate,
decoration={
name=shape backgrounds,
shape=circle,
shape size={#1},
pgfplots y shape sep={#2}}}]
\begin{axis}[
width=15cm, axis lines=middle,
xmin=-1, xmax=19,
ymin=-1, ymax=16,
xtick={0,...,18}, ytick ={0,...,15},
xticklabels={0,...,18}, yticklabels={0,...,15},
axis line style={very thick, ->},
x label style={at={(ticklabel* cs:1)}, anchor=west,},
y label style={at={(ticklabel* cs:1)}, anchor=south},
xlabel={$x$}, ylabel={$y$},
unit vector ratio*=1 1 1
]
\draw[very thin, pgfplots step=1 by 1] (0,0) grid (5,5);
\draw[decorate sep={2mm}{1}, fill] (0,1) -- (0,4);
%\pgfplotsinvokeforeach{1,...,4}{
% \node[shape=circle, inner sep=+0pt, minimum size=2mm, draw, fill] at (0,#1) {};
%}
\end{axis}
\end{tikzpicture}
\end{document}