PGFPlots:奇怪的行为

PGFPlots:奇怪的行为

看看以下两个图之间的区别。我希望图“1”中的颜色条、轴标签和刻度标签之间的距离与图“2”中的距离相同。

如何实现这一点?

在此处输入图片描述

\documentclass[a4paper,12pt]{article}
\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=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
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={0,0.05,...,0.3},
minor xtick={0,0.01,...,0.2},
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
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={0,0.05,...,0.3},
    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)},
legend cell align=left]
\end{axis}
\end{tikzpicture}
\end{figure}


\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
yticklabel shift=2pt,
xticklabel shift=2pt,
xmin=-6, xmax=6,
ymin=0, ymax=6,
point meta min={-6},
point meta max={6},
xlabel=\large $m$,
ylabel=\large $\Delta l$,
colorbar horizontal,
colorbar style={
    /pgf/number format/fixed,
    xticklabel shift=2pt,
    xtick={-6,-4,...,6},
    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)}
]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

答案1

灰色轴线的问题出现是因为主网格线与轴线重合。除了在顶部放置轴线,您还可以在 end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);} 之后使用轴线 style={draw=none}。x 标签仍然有小偏移,这是由于额外的 x 刻度引起的(尽管不应该如此)。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%感谢杰克%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[/pgf/number format/fixed,
legend pos=outer north east,
grid=major,
xmin=0, xmax=0.2,
ymin=0, ymax=0.6,
point meta min={0},
point meta max={0.2},
yticklabel shift=2pt,
xticklabel shift=2pt,
legend pos=north west,
%minor tick num=4,
xticklabel style={text height=1.5ex},
xtick={0,0.05,...,0.3},
%minor xtick={0,0.01,...,0.2},
%minor ytick={0,0.02,...,0.6},
extra x ticks={0,0.05,...,0.3},
extra x tick style={
    xticklabel pos=right,
    xticklabel style={text depth=0pt}
},
extra y ticks={0,0.1,...,0.7},
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,
    xtick={0,0.05,...,0.3},
    xticklabel shift=2pt,
    xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)},
legend cell align=left]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

相关内容