PGFPlots 交错轴

PGFPlots 交错轴

为什么在这个图中轴线都是交错的?

\documentclass[12pt,%                      % corpo del font principale
                   a4paper,%                   % carta A4
    %               draft,%                     % evidenzia le righe Overfull hbox
                   twoside,%
    %               oneside,%
    %               openright,%                % fronte-retro
                   openany%                    % solo fronte
                   ]{book}


    \usepackage{pgfplots,pgfplotstable}        % Grafici
    \pgfplotsset{/pgf/number format/use comma,compat=newest,%
                width=12cm,%
                height=9cm%
                }

    \begin{document}
    \begin{figure}
    \centering
    \begin{tikzpicture}
    \makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
    \pgfplotsset{axis line on top/.style={
     axis on top=false,
     after end axis/.append code={
        \pgfplotsset{axis line style=opaque,
          ticklabel style=opaque,
          tick style=opaque,
          grid=none}
        \pgfplotsdrawaxis}
      }
    }
    \begin{axis}[/pgf/number format/fixed,
    legend pos=outer north east,
    grid=major,
    xmin=80, xmax=100,
    ymin=0, ymax=100,
    point meta min={80},
    point meta max={100},
    yticklabel shift=2pt,
    xticklabel shift=2pt,
    legend pos=north west,
    minor tick num=4,
    axis line on top,
    xticklabel style={text height=1.5ex},
    xtick={80,0.5,...,100},
    minor xtick={80,0.05,...,100},
    extra x ticks={80,0.5,...,100},
    extra x tick style={
        xticklabel pos=right,
        xticklabel style={text depth=0pt}
    },
    extra y ticks={0,20,...,100},
    extra y tick style={
        yticklabel pos=right
    },
    tick style={thin,black},
    xlabel=\large $m$,
    ylabel=\large $\Delta l$,
    colorbar horizontal,
    colorbar style={
        /pgf/number format/fixed,
        xticklabel shift=2pt,
        xtick={80,0.5,...,100},
        xticklabel style={text height=1.5ex}
    },
    colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
    after end axis/.append code={
        \draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
    },
    legend cell align=left]
    \end{axis}
    \end{tikzpicture}
    \caption{Grafico dei dati.}
    \label{fig:Grafico_ottica}
    \end{figure}
    \end{document}

答案1

发生这种情况的原因是,您尝试在 处绘制一条线x=0,但 x 值实际上不在您的图上。您可以通过两种方式纠正此问题:

  • 更改after end axis/.codebefore end axis/.code。这样,线条将从图中剪掉,并且不会影响边界框。
  • 儘量移除整個after end axis/.code={...}

\documentclass[12pt,%                      % corpo del font principale
                   a4paper,%                   % carta A4
    %               draft,%                     % evidenzia le righe Overfull hbox
                   twoside,%
    %               oneside,%
    %               openright,%                % fronte-retro
                   openany%                    % solo fronte
                   ]{book}
\usepackage{pgfplots,pgfplotstable}        % Grafici
\pgfplotsset{/pgf/number format/use comma,compat=newest,%
            width=12cm,%
            height=9cm%
            }

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
\pgfplotsset{axis line on top/.style={
 axis on top=false,
 after end axis/.append code={
    \pgfplotsset{axis line style=opaque,
      ticklabel style=opaque,
      tick style=opaque,
      grid=none}
    \pgfplotsdrawaxis}
  }
}
\begin{axis}[/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=80, xmax=100,
ymin=0, ymax=100,
point meta min={80},
point meta max={100},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
minor tick num=4,
axis line on top,
xticklabel style={text height=1.5ex},
xtick={80,0.5,...,100},
minor xtick={80,0.05,...,100},
extra x ticks={80,0.5,...,100},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,20,...,100},
extra y tick style={
    yticklabel pos=right
},
tick style={thin,black},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={80,0.5,...,100},
    xticklabel style={text height=1.5ex}
},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
before end axis/.append code={
    \draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
},
legend cell align=left]
\end{axis}
\end{tikzpicture}
\caption{Grafico dei dati.}
\label{fig:Grafico_ottica}
\end{figure}
\end{document}

相关内容