我希望自动化(简化)我的 MWE 上用蓝色绘制的路径。我尽量避免使用节点,因为它嵌套在 中tikzpicture
。
理想情况下,我希望简单地指定开始和结束坐标,并调整步长,也许还有振幅。也许使用pgfplots
这是我的 MWE 的结果:(糟糕的截图)
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{snakes,backgrounds,arrows,decorations.markings}
\tikzset{rbox/.style= {draw=black,fill=white,thick,rectangle,rounded corners,inner xsep=2pt, inner ysep=2pt},
fancytitle/.style={fill=blue!15,draw=black,thick,text=black,rectangle,rounded corners},}
\begin{document}
\begin{tikzpicture}[transform shape]
\node [rbox] (box) {
\begin{minipage}{.8\linewidth}
{
\begin{center}
\begin{tikzpicture}[line width=1pt,sharp corners,x=1cm,y=1cm,
every node/.style={font=\footnotesize}]
\clip (0,-0.5) rectangle (6,6);
\draw [help lines] (0,0) grid (6,6);
% Axes
\draw [<->,>=triangle 45] (0,3) -- (6,3);
\draw [<->,>=triangle 45] (3,0) -- (3,6);
\draw [decoration={ticks,amplitude=.75mm,segment length=5mm},decorate,line width=0.75pt]
(0.5,3) -- (5.5,3);
\draw [decoration={ticks,amplitude=.75mm,segment length=5mm},decorate,line width=0.75pt]
(3,0.5) -- (3,5.5);
% Line
\draw [*-*] (0.5,0.5) -- (5.5,5.5);
% Counting
\draw [->,line width=1pt,blue] (0.5,0.5) .. controls (.65,0) and (.85,0) .. (1,0.5)
.. controls (1.15,0) and (1.35,0) .. (1.5,0.5)
.. controls (1.65,0) and (1.85,0) .. (2,0.5);
\end{tikzpicture}
\end{center}
}
\end{minipage}
};
\node[fancytitle,right=8pt] at (box.north west) {Grid:};
\end{tikzpicture}
\end{document}
答案1
to
除了提供控制点之外,您还可以使用选项和bend right=<angle>
来指定曲线looseness=<...>
。我将以下内容添加到您的代码中:
\draw [->,line width=1pt,red] (0.5,0.5)
to[bend right=80,looseness=2.5] (1,0.5)
to[bend right=80,looseness=2.5] (1.5,0.5)
to[bend right=80,looseness=2.5] (2,0.5);
如果您想要更多自动化,您可以让 TikZ 计算给定值和值之间的 x 值序列 、1
,如下所示:1.5
2
\startx
\step
\def\startx{0.5}
\def\starty{0.5}
\def\step{0.5}
\def\finishx{2}
\pgfmathsetmacro\nextx{\startx+\step}
\pgfmathsetmacro\nextnextx{\startx+2*\step}
\draw [->,line width=1pt,red] (\startx,\starty)
\foreach \x in {\nextx,\nextnextx,...,\finishx} {
to[bend right=80,looseness=2.5] (\x,0.5)
};
无论如何,结果是:
更新。我注意到你的黑色斑点在各自的网格方块中并不完全居中。这是因为 TikZ 绘制箭头时,结尾箭头尖端是指定点。对于你的情况,我假设你想要中心箭头尖端位于指定点。您可以通过将斑点绘制为节点,然后用线连接它们来解决此问题,如下所示:
%\draw [*-*] (0.5,0.5) -- (5.5,5.5);
\node[shape=circle,fill=black,outer sep=0mm] (a) at (0.5,0.5) {};
\node[shape=circle,fill=black,outer sep=0mm] (b) at (5.5,5.5) {};
\draw (a) -- (b);
更新 2。要得到...
- 延伸的附加弧序列垂直从同一个起点出发,
- 每条弧上的数字标签
...您可以使用以下代码。
\def\startx{0.5}
\def\starty{0.5}
\def\stepx{0.5}
\def\stepy{0.25}
\def\finishx{2}
\def\finishy{2.25}
\pgfmathsetmacro\nextx{\startx+\stepx}
\pgfmathsetmacro\nextnextx{\startx+2*\stepx}
\pgfmathsetmacro\nexty{\starty+\stepy}
\pgfmathsetmacro\nextnexty{\starty+2*\stepy}
\draw [->,line width=1pt,red] (\startx,\starty)
\foreach [count=\i] \x in {\nextx,\nextnextx,...,\finishx} {
to[bend right=80,looseness=2.5] node[green, auto, swap] {\tiny\i} (\x,\starty)
};
\draw [->,line width=1pt,red] (\startx,\starty)
\foreach [count=\i] \y in {\nexty,\nextnexty,...,\finishy} {
to[bend left=80,looseness=2.5] node[green, auto] {\tiny\i} (\startx,\y)
};