如何在 pgfplots 中生成多分辨率点网格(见图)?点网格不一定与轴刻度重合。
编辑:上面的图是使用
\addplot[only marks,mark=*] plot table[row sep=crcr,]{ high res. coordinates }
\addplot[only marks,mark=*] plot table[row sep=crcr,]{ low res. coordinates }
带有大量 MATLAB 计算坐标表。我在这个问题中寻找的是一种在 pgfplots 中以算法方式创建点网格的方法。
答案1
和tikz
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (-2.6,-2.6) rectangle (2.6,2.6);
\foreach \x in {-2,-1,...,2}{
\draw (\x,-2.4) -- (\x,-2.6)node[below,font=\footnotesize] {\x};
\draw (\x,2.4) -- (\x,2.6);
}
\foreach \y in {-2,-1,...,2}{
\draw (-2.4,\y) -- (-2.6,\y)node[left,font=\footnotesize] {\y};
\draw (2.6,\y) -- (2.4,\y);
}
\foreach \x in {-2.4,-2.2,...,2.4}{
\foreach \y in {-2.4,-2.2,...,2.4}{
\fill[black!30] (\x,\y) circle[radius=0.5pt];
}}
\foreach \x in {-2.4,-1.8,...,2.4}{
\foreach \y in {-2.4,-1.8,...,2.4}{
\fill[black!80] (\x,\y) circle[radius=0.8pt];
}}
\end{tikzpicture}
\end{document}
使用pgfplots
。
我采纳了 Tarass 的答案,并将 fots 添加到背景中,以便情节凸显出来。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-2.6,xmax=2.6,
ymin=-2.6,ymax=2.6,
after end axis/.code={%
\begin{scope}[on background layer]
\foreach \x in {-2.4,-2.2,...,2.4}{
\foreach \y in {-2.4,-2.2,...,2.4}{
\fill[black!30] (axis cs:\x,\y) circle[radius=0.5pt];
}}
\foreach \x in {-2.4,-1.8,...,2.4}{
\foreach \y in {-2.4,-1.8,...,2.4}{
\fill[black!80] (axis cs:\x,\y) circle[radius=0.8pt];
}}
\end{scope}
},]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
但这比 Christian Feuersänger 的方法要慢一些。
使用\addplot
(更慢)
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-2.6,xmax=2.6,
ymin=-2.6,ymax=2.6,
]
\foreach \x in {-2.4,-2.2,...,2.4}{
\foreach \y in {-2.4,-2.2,...,2.4}{
\addplot[only marks,mark size=0.5pt,black!30] coordinates {(\x,\y)} ;
}}
\foreach \x in {-2.4,-1.8,...,2.4}{
\foreach \y in {-2.4,-1.8,...,2.4}{
\addplot[only marks,mark size=0.8pt,black!80] coordinates {(\x,\y)} ;
}}
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
最后在坐标上画上黑点(基本上由 Harish Kumar 完成):
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.9}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-2.6,xmax=2.6,
ymin=-2.6,ymax=2.6,
after end axis/.code={%
\begin{scope}[on background layer]
\foreach \x in {-2.6,-2.4,...,2.6}{
\foreach \y in {-2.6,-2.4,...,2.6}{
\fill[black!30] (axis cs:\x,\y) circle[radius=0.5pt];
}}
\foreach \x in {-2,...,2}{
\foreach \y in {-2,...,2}{
\fill[black!80] (axis cs:\x,\y) circle[radius=0.8pt];
}}\end{scope}
}]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
由于您明确指出点不需要与坐标系匹配,因此您可以简单地使用填充来pattern
填充背景。
这是获得所需内容的最有效方法 - 它可以缩放到任意大小,而无需放大生成的 PDF。显示速度也会更快。
\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfdeclarepatternformonly{pattern}
{\pgfpoint{-0.3mm}{-0.3mm}}%% BB lower left
{\pgfpoint{1cm}{1cm}}% BB upper right
{\pgfpoint{1cm}{1cm}}% tile size
{
\foreach \x in {0,0.3333333,0.66666} {%
\foreach \y in {0,0.333333,0.6666666} {%
\def\radius{0.1mm}%
\ifdim\x pt=0pt
\ifdim\y pt =0pt
\def\radius{0.3mm}%
\fi
\fi
\pgfpathcircle{\pgfqpoint{\x cm}{\y cm}}{\radius}%
\pgfusepath{fill}%
}%
}%
%\pgfpathrectangle\pgfpointorigin{\pgfqpoint{1cm}{1cm}}\pgfusepath{stroke}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-2,xmax=2,
ymin=-2,ymax=2,
axis background/.style={pattern=pattern},
]
\end{axis}
\end{tikzpicture}
\end{document}
请注意,这与完全无关pgfplots
。