我正在尝试对上面的图片进行编码,特别是矩形内的点。
这是我现在的代码,但它看起来不像预期的图形。我该如何更改代码以获得所需的输出?
\documentclass[11pt,a5paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[thick,->] (0,0) -- (4.5,0);
\draw[thick,->] (0,0) -- (0,4.5);
\foreach \x in {0,1,2,3,4}
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {0,1,2,3,4}
\draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};
\draw [ultra thick,blue] (2,0) rectangle (3,2);
\node at (2.5,1){patch};
\end{tikzpicture}
\end{document}
答案1
有点笨拙,但如果需要随机性,那么这种path picture
方法是可行的。请注意,对于低稀疏性,编译时间会急剧增加。解决此问题的一个方法是创建一个单独的大型独立文档,然后进行\includegraphics
修剪。
\documentclass[tikz,border=5]{standalone}
\tikzset{
random dots/.style={
path picture={
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}%
\pgfgetlastxy\pathwidth\pathheight
\tikzset{shift=(path picture bounding box.center)}
\def\sparseness{#1}
\pgfmathparse{int(\pathwidth/\sparseness+2)}\let\m=\pgfmathresult
\pgfmathparse{int(\pathheight/\sparseness+2)}\let\n=\pgfmathresult
\foreach \i in {-\m,...,\m}
\foreach \j in {-\n,...,\n}
\fill [shift={(\i*\sparseness,\j*\sparseness)}, shift={(rand*\sparseness/2,rand*\sparseness/2)}, every random dot/.try]
(0,0) circle [radius=1pt];
}
},
random dots/.default=5pt,
every random dot/.style={fill=red}
}
\begin{document}
\begin{tikzpicture}
\draw [ultra thick,blue, random dots] (0,0) rectangle ++(1,2);
\draw [ultra thick,blue, random dots=10pt] (2,0) rectangle ++(1,2);
\draw [ultra thick,blue, random dots=2pt] (4,0) rectangle ++(1,2);
\end{tikzpicture}
\end{document}
答案2
实现随机性的另一种方法是使用波浪装饰(原始图片中的那些“东西”在我看来就像小波浪)和循环:
\documentclass[11pt,a5paper]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,fit}
\begin{document}
\begin{tikzpicture}[decoration={waves,radius=.8mm, segment length=2mm, angle=5mm}]
\draw[thick,->] (0,0) -- (4.5,0);
\draw[thick,->] (0,0) -- (0,4.5);
\foreach \x in {0,1,2,3,4}
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {0,1,2,3,4}
\draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};
\foreach \i in {290,110,80,40}
\foreach \l in {250,90,330}
\foreach \m in {1,7}{
\node(a)[blue,fit={(2,0) (3,2)},inner sep=-\m pt]{};
\draw [thick,decorate,red,line cap=round](a.\l) to (a.\i);
}
\draw [ultra thick,blue] (2,0) rectangle (3,2) node (b)[inner xsep=0pt,inner ysep=2pt,midway,fill=white] {patch};
\draw [blue,<->,thick](b.south west)--(b.south east);
\end{tikzpicture}
\end{document}
答案3
patterns
这里是使用库的快速尝试tikz
。绘制此图像的可能性要多得多。我只是为patch
标签使用了白色背景,但可能还有更复杂的解决方案。例如,可以选择反转剪辑,使patch
标签周围的图案褪色为白色/不透明。
此外,我扔掉了你的一个循环,因此轴刻度是在一个循环内绘制的。
\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
% axes and x-/y-ticks
\draw [>=latex, <->, thick] (0,5) -- (0,0) -- (5,0);
\foreach \x in {1,...,4} {
\draw (\x,-2pt) -- ++(0,4pt) node [below=.1cm] {\scriptsize $\x$};
\draw (-2pt,\x) -- ++(4pt,0) node [left=.1cm] {\scriptsize $\x$};
}
% rectangle with pattern and patch
\draw [ultra thick, blue, fill, pattern=crosshatch dots, pattern color=red] (2,0) rectangle (3,2);
\draw [thick, blue, >=latex, <->] (2,1) -- (3,1) node [midway, above=.1cm, fill=white, inner sep=0pt] {\scriptsize patch};
% x-labels
\draw [thick, dashed] (2.5,0) -- ++(0,-.5) node [below] {$X_1$};
\draw [thick, dashed] (0,0) -- ++(0,-.5) node [below] {$X_0=0$};
\end{tikzpicture}
\end{document}
渲染图像:
编辑更改颜色
最初的问题是如何使用特殊颜色这里有一些关于颜色的附加信息。
如果你在或执行时使用dvipsnames
as 选项,则可以使用-colors。有关概述,请访问\documentclass
\usepackage[usenames,dvipsnames]{color}
dvips
此链接。
如果你需要更好地控制颜色,你可以使用例如
red
red!75
red!75!blue
设置颜色的饱和度或混合颜色。
答案4
看起来是我的另一个用例泊松圆盘采样库:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{poisson}
\usetikzlibrary{calc}
\begin{document}
\tikzset{ % Define the shape of each little mark
pattern/.pic = {
\draw[red!50!black] (-0.1,0) to[out=360*rnd,in=360*rnd] (0.1*rnd,0);
}
}
\begin{tikzpicture}
\draw[thick,->] (0,0) -- (4.5,0);
\draw[thick,->] (0,0) -- (0,4.5);
\foreach \x in {0,1,2,3,4}
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {0,1,2,3,4}
\draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};
% Generate the random coordinates for each little mark, using
% poisson disc sampling (over a rectangle of 1x2, minimum distance
% between marks of 0.1)
\edef\mylist{\poissonpointslist{1}{2}{0.1}{25}}
\foreach \x/\y in \mylist {
\pgfmathsetmacro{\angle}{60*rnd}
\path ($(\x,\y)+ (2,0)$) pic[scale=0.4,rotate=\angle] {pattern};
}
\draw [ultra thick,blue] (2,0) rectangle +(1,2);
\node[fill=white, inner sep=1pt] at (2.5,1){patch};
\end{tikzpicture}
\end{document}
使用lualatex
最新的 pgf 库进行编译,您将获得: