Tikzpicture 与 [带坐标的网格] 水平对齐

Tikzpicture 与 [带坐标的网格] 水平对齐

我尝试水平对齐三个网格,其中两个在一个图中。以下是示例代码:

\documentclass[twocolumn]{article}
%% -------------------------------------------------------------------------------------
\usepackage{tikz,pgfplots}

\makeatletter
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\tikzset{
  grid with coordinates/.style={
    to path={%
      \pgfextra{%
        \edef\grd@@target{(\tikztotarget)}%
        \tikz@scan@one@point\grd@save@target\grd@@target\relax
        \edef\grd@@start{(\tikztostart)}%
        \tikz@scan@one@point\grd@save@start\grd@@start\relax
        \draw[minor help lines] (\tikztostart) grid (\tikztotarget);
        \draw[major help lines] (\tikztostart) grid (\tikztotarget);
        \grd@start
        \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
        \grd@target
        \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
        \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step x}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step y}}
        \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
        \node[anchor=north] at (\x,\grd@ya -0.05) {\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\x}};       
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\y}};
      }
    }
  },
  minor help lines/.style={
    help lines,
    black!20!white,
    line cap =round,
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step y}
  },
  major help lines/.style={
    help lines,
    black!20!white,
    line cap =round,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/major step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/major step y}
  },
  grid with coordinates/.cd,
  minor step x/.initial=.5,
  minor step y/.initial=.2,
  major step x/.initial=1.0,
  major step y/.initial=1.0,
  major line width/.initial=1pt,
  grid precision x/.initial=1,
}
\makeatother
%% -------------------------------------------------------------------------------------

\begin{document}

\begin{figure}[!b]
    \centering
    \begin{tikzpicture}[xscale=0.1,yscale=1,font=\scriptsize,grid with coordinates/major step x=10,grid with coordinates/minor step x=2]
        \draw (0,0) to[grid with coordinates] (50,1);

        \coordinate [label=above:``low''] (A) at (5,1.0);
        \draw[thick,line cap =round] (0,1) -- (10,1) -- (20,0);
        \coordinate [label=above:``average''] (A) at (20,1.0);
        \draw[thick,line cap =round] (10,0) -- (20,1) -- (30,0);        
        \coordinate [label=above:``high''] (A) at (35,1.0);
        \draw[thick,line cap =round] (20,0) -- (30,1) -- (50,1);

    \end{tikzpicture}
    \begin{tikzpicture}[xscale=1,yscale=1,font=\scriptsize,grid with coordinates/minor step x=0.2]
        \draw (0,0) to[grid with coordinates] (5,1);

        \coordinate [label=above:``low''] (A) at (0.5,1.0);
        \draw[thick,line cap =round] (0,1) -- (1,1) -- (2,0);
        \coordinate [label=above:``average''] (A) at (2,1.0);
        \draw[thick,line cap =round] (1,0) -- (2,1) -- (3,0);       
        \coordinate [label=above:``high''] (A) at (3.5,1.0);
        \draw[thick,line cap =round] (2,0) -- (3,1) -- (5,1);

    \end{tikzpicture}
    \caption{Membership functions of 12-month ROR ($C$ -- upper) and 1-month ROR ($P$ -- lower).}\label{fig:predicatesRORmemF}
\end{figure}
\begin{figure}[!b]
    \centering
    \begin{tikzpicture}[xscale=5,yscale=1,font=\scriptsize,,grid with coordinates/major step x=0.2,grid with coordinates/minor step x=0.04]
        \draw (0,0) to[grid with coordinates] (1,1);

        \coordinate [label=above:${Q}_1$ -- ``majority''] (A) at (0.5,1.0);
        \draw[thick,dashed,line cap =round] (0.3,0) -- (0.5,1) -- (1.0,1) -- (1.0,0);
        \coordinate [label=above:${Q}_2$ -- ``most''] (A) at (0.90,1.0);
        \draw[thick,line cap =round] (0.3,0) -- (0.8,1) -- (1.0,1) -- (1.0,0);      

    \end{tikzpicture}
    \caption{${Q}_1$ (``majority'') and ${Q}_2$ (``most'') linguistic quantifiers' membership functions.}\label{fig:quantifiersMemF}
\end{figure}

\end{document}

以及生成的 PDF 的屏幕截图:

有效的 XHTML

据我所知,细微的差别来自于“5.0”和“50.0”的宽度。

我试过了

\pgfresetboundingbox
\path (-10.1) rectangle (60.0);

\path (-1.1) rectangle (6.0);

tikzpicture 对齐和居中没有结果。

这个问题有没有什么简单的解决办法?我只需要在那 3 个网格/2 个图形上使用它。

答案1

您可能,不希望.将 用作坐标分隔符。

以下对我有用:\path (-10, 0) -- (60, 0);在第一张图和\path (-1, 0) -- (6, 0)第二张图中。

答案2

只需在每个 tikzpicture 环境中添加边界框规范即可。我在下面修改了您的代码。我添加了一条垂直红线来指示对齐。

\documentclass[twocolumn]{article}
%% -------------------------------------------------------------------------------------
\usepackage{tikz,pgfplots}

\makeatletter \def\grd@save@target#1{% \def\grd@target{#1}} \def\grd@save@start#1{% \def\grd@start{#1}} \tikzset{ grid with coordinates/.style={ to path={% \pgfextra{% \edef\grd@@target{(\tikztotarget)}% \tikz@scan@one@point\grd@save@target\grd@@target\relax \edef\grd@@start{(\tikztostart)}% \tikz@scan@one@point\grd@save@start\grd@@start\relax \draw[minor help lines] (\tikztostart) grid (\tikztotarget); \draw[major help lines] (\tikztostart) grid (\tikztotarget); \grd@start \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm} \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm} \grd@target \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm} \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm} \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step x}} \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step y}} \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb} \node[anchor=north] at (\x,\grd@ya -0.05) {\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\x}};
\foreach \y in {\grd@ya,\grd@yc,...,\grd@yb} \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\y}}; } } }, minor help lines/.style={ help lines, black!20!white, line cap =round, xstep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step x}, ystep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step y} }, major help lines/.style={ help lines, black!20!white, line cap =round, line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width}, xstep=\pgfkeysvalueof{/tikz/grid with coordinates/major step x}, ystep=\pgfkeysvalueof{/tikz/grid with coordinates/major step y} }, grid with coordinates/.cd, minor step x/.initial=.5, minor step y/.initial=.2, major step x/.initial=1.0, major step y/.initial=1.0, major line width/.initial=1pt, grid precision x/.initial=1, } \makeatother %% ------------------------------------------------------------------------------------- \pgfplotsset{compat=1.8}

\begin{document}

\begin{figure}[!h] \centering \begin{tikzpicture}[xscale=0.1,yscale=1,font=\scriptsize,grid with coordinates/major step x=10,grid with coordinates/minor step x=2] \useasboundingbox (0,-1) rectangle (50,1); \draw (0,0) to[grid with coordinates] (50,1); \coordinate [label=above:low''] (A) at (5,1.0); \draw[thick,line cap =round] (0,1) -- (10,1) -- (20,0); \coordinate [label=above:average''] (A) at (20,1.0); \draw[thick,line cap =round] (10,0) -- (20,1) -- (30,0);
\coordinate [label=above:high''] (A) at (35,1.0); \draw[thick,line cap =round] (20,0) -- (30,1) -- (50,1); \end{tikzpicture} \begin{tikzpicture}[xscale=1,yscale=1,font=\scriptsize,grid with coordinates/minor step x=0.2] \useasboundingbox (0,-0.3) rectangle (5,1); \draw (0,0) to[grid with coordinates] (5,1); \coordinate [label=above:low''] (A) at (0.5,1.0); \draw[thick,line cap =round] (0,1) -- (1,1) -- (2,0); \coordinate [label=above:average''] (A) at (2,1.0); \draw[thick,line cap =round] (1,0) -- (2,1) -- (3,0);
\coordinate [label=above:
high''] (A) at (3.5,1.0); \draw[thick,line cap =round] (2,0) -- (3,1) -- (5,1); \end{tikzpicture} \caption{Membership functions of 12-month ROR ($C$ -- upper) and 1-month ROR ($P$ -- lower).}\label{fig:predicatesRORmemF} \end{figure} \begin{figure}[!h] \centering \begin{tikzpicture}[xscale=5,yscale=1,font=\scriptsize,,grid with coordinates/major step x=0.2,grid with coordinates/minor step x=0.04] \useasboundingbox (0,-0.3) rectangle (1,1); \draw (0,0) to[grid with coordinates] (1,1); \coordinate [label=above:${Q}_1$ -- majority''] (A) at (0.5,1.0); \draw[thick,dashed,line cap =round] (0.3,0) -- (0.5,1) -- (1.0,1) -- (1.0,0); \coordinate [label=above:${Q}_2$ --most''] (A) at (0.90,1.0); \draw[thick,line cap =round] (0.3,0) -- (0.8,1) -- (1.0,1) -- (1.0,0); \end{tikzpicture} \caption{${Q}_1$ (majority'') and ${Q}_2$ (most'') linguistic quantifiers' membership functions.}\label{fig:quantifiersMemF} \end{figure} \begin{tikzpicture}[red] \useasboundingbox (0,0) rectangle (0,0); \draw (6.19,0) -- +(0,10); \end{tikzpicture} \end{document}

相关内容