我正在努力在乳胶代码中放置标签和不填充圆圈。我希望得到以下输出:
我设法得出了以下代码:
\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}[
PC/.style={black, thick},
dot/.style={circle,fill=black,minimum size=4pt,inner sep=0pt,outer sep=-1pt}
]
% Define linear parameters
\def\PCint{5} %Y-intercept
\def\PCslp{-1} %Slope
\def\PC{\x,{\PCslp*\x+\PCint}}
\draw[PC, domain=0:5]
plot (\PC);
% Define linear parameters
\def\PCint{4} %Y-intercept
\def\PCslp{-1} %Slope
\def\PC{\x,{\PCslp*\x+\PCint}}
\draw[PC, domain=0:5]
plot (\PC)
node [right] {margin};
\draw [domain=0:5]
let
\p1=(61.28041pt, 26.23682pt),
\n1={\y1-\PCslp*\x1} in
% points the plot should pass through...
node [dot] at (\x1,\y1) {}
node [dot] at (0,\n1) {}
node [dot] at (0,1) {}
node [dot] at (1,0) {}
node [dot] at (1.3,0.5) {}
node [dot] at (1.7,0) {}
node [dot] at (3,-1) {}
node [dot] at (4,-2) {}
% first try for a plot...
plot[yshift=\n1] (\x,\PCslp*\x)
node [right,yshift=\n1] {$-2/||w||$};
\end{tikzpicture}
\end{document}
我恳请您帮助我如何才能最好地改进我的图表。
答案1
以下是实现此目的的方法。主要思想:
定义一些坐标
使用极坐标(angle:radius)
。因此,主要参考点是线 H 的起点。
% ~~~ start coordinates A, A1, A2; B == direction
\coordinate (A) at (0,0);
\coordinate (A1) at (60:7mm);
\coordinate (A2) at (240:7mm);
\coordinate (B) at (-30:5cm);
使用路径上的 pos
- 将节点向左移动一点以标记线
- 画一条线,在这里
(A)
和之间(B)
- 在末尾放置一个不可见的节点,以便稍后处理边距问题
% ~~~ one longer path ~~~~
\draw
node[xshift=-4mm] {$H$}
(A) -- (B)
node(X)[pos=1] {} % invisible node at end
;
还可以将圆圈放在其他路径上,就像node[pos=.2,dc] {} % the circles, shifted along path
使用其中一种定义的样式一样。
样式
这些风格是逐步引入的,并且经过一定程度的重复使用。
c/.style={circle,draw},% open circles
cf/.style={c,fill=black},% filled circles
使用隐藏节点
...稍后绘制箭头并放置边距标签。
% ~~~ arrows below ~~~~~~~
\draw [<->] (X1) -- (X2);
% ~~~ label below ~~~~~~
\node[align=center,xshift=8mm,yshift=-2mm] at (X)
{margin\\$\frac{-2}{\|\omega\|}$};
考虑align=center
格式选项中的多行使用,并在所需的换行符\\
内。{ }
最后 ...
使用极坐标放置一些点,参考“H”。
\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
c/.style={circle,draw},% open circles
cf/.style={c,fill=black},% filled circles
dc/.style={c,double},% upper circles
dcf/.style={dc,fill=black},% lower circles
> = {Stealth}% arrow heads
]
% ~~~ start coordinates A, A1, A2; B == direction
\coordinate (A) at (0,0);
\coordinate (A1) at (60:7mm);
\coordinate (A2) at (240:7mm);
\coordinate (B) at (-30:5cm);
% ~~~ one longer path ~~~~
\draw
node[xshift=-4mm] {$H$}
(A) -- (B)
node(X)[pos=1] {} % invisible node at end
;
% ~~~ upper parallel ~~~~~~~~~~~~~
\draw
(A1) node[xshift=-3mm] {$H_1$}
-- +(B)% using coordinate B as relative coordinate
node[pos=.2,dc] {} % the circles, shifted along path
node[pos=.7,dc] {}
node(X1)[pos=.95,inner sep=0] {} % invisible node
;
% ~~~ lower parallel ~~~~~~~~~~~~~
\draw
(A2) node[xshift=-3mm] {$H_2$}
-- +(B)
node[pos=.2,dcf] {} % filled
node[pos=.7,dcf] {}
node(X2)[pos=.95,inner sep=0] {} % invisible node
;
% ~~~ arrows below ~~~~~~~
\draw [<->] (X1) -- (X2);
% ~~~ label below ~~~~~~
\node[align=center,xshift=8mm,yshift=-2mm] at (X)
{margin\\$\frac{-2}{\|\omega\|}$};
% ~~~ some dots ~~~~~~~
\draw (30:1.5) node[c] {}
(5:2.5) node[c] {}
(8:4) node[c] {}
(-12:5) node[c] {}
;
\draw (270:2) node[cf] {}
(300:2) node[cf] {}
(305:3) node[cf] {}
(312:4) node[cf] {}
;
\end{tikzpicture}
\end{document}
答案2
这是另一种解决方案,通过指定沿线的比例和与线的距离(边界和线之间的距离)将点相对于线放置。
节点被放置明确地通过距离的一小部分。距离使用line direction
与我们指定图表角度相同的键。
如果您不喜欢线条的角度,请更改-30
内部line direction
的定义(但是,H
如果您改变太多,节点以及边缘节点将需要进行一些调整)。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\tikzset{
fake double/.style 2 args={
label={[#1, minimum size={#2}]center:}}}
\begin{document}
\begin{tikzpicture}[
o/.style={shape=circle, draw, inner sep=+0pt, minimum size=+2mm},
O/.style={o, fake double={o}{+2.6mm}},
c/.style={o, fill, draw=none},
C/.style={o, fill, fake double={o}{+2.6mm}},
>=Latex,
line direction/.style={rotate=-30, shift={(90:#1)}}]
\foreach \i/\t in {-1/H_2, 0/H, 1/H_1}
\draw[line direction=\i*.75]
(0, 5) coordinate (st\i) node[above left]{$\t$}
-- coordinate[pos=.95] (mk\i) +(right:5) coordinate (en\i);
\node[below right, align=center] at (en0) {margin\\$-2/\|\omega\|$};
\draw[<->] (mk-1) -- (mk1);
\path (st-1) -- (en-1)
node foreach \Pos in {.3333, .8} [C, pos=\Pos]{}
node foreach \Pos/\Dist in {.2/5, .45/2, .7/2, .4/8, .65/7, .85/4}
[c, below, pos=\Pos, line direction=-\Dist mm] {};
\path (st1) -- (en1)
node foreach \Pos in {.3333, .8} [O, pos=\Pos]{}
node foreach \Pos/\Dist in {.2/5, .4/2, .45/8, .7/7, .85/5}
[o, above, pos=\Pos, line direction=\Dist mm] {};
\end{tikzpicture}
\end{document}