在 tikzpicture 的绘图周围添加轴

在 tikzpicture 的绘图周围添加轴

我有一个图形,并绘制了一些值。我的问题是,我似乎无法指定图形应在所有边上都有轴。

我的身材:

在此处输入图片描述

我希望有如下图所示的边框。这意味着整个图周围都有轴:

在此处输入图片描述

我的代码如下:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{tikz}
\usepackage{biblatex}
\usepackage{subfigure}
\addbibresource{biblatex-examples.bib}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{filecontents}
\usetikzlibrary{positioning,shapes}
\begin{filecontents}{data_ape_dist.dat}
x,y,y2
0,0,0
0.05,2.021,1.781
0.1,1.928,1.734
0.15,1.798,1.634
0.2,1.567,1.45
0.25,1.316,1.274
0.3,1.167,1.177
0.35,0.948,0.993
0.4,0.744,0.869
0.45,0.652,0.718
0.5,0.492,0.57
0.55,0.428,0.465
0.6,0.304,0.389
0.65,0.256,0.272
0.7,0.203,0.229
0.75,0.133,0.178
0.8,0.092,0.141
0.85,0.058,0.091
0.9,0.035,0.063
0.95,0.036,0.051
1,0.021,0.043
1.05,0.012,0.026
1.1,0.014,0.031
1.15,0.011,0.018
1.2,0.01,0.012
1.25,0.012,0.022
1.3,0.012,0.012
1.35,0.006,0.006
1.4,0.013,0.005
1.45,0.009,0.008
1.5,0.003,0.006
1.55,0.004,0.007
1.6,0.001,0.009
1.65,0.001,0.002
1.7,0.002,0.003
1.75,0,0.003
1.8,0.001,0.005
1.85,0.001,0.002
1.9,0,0.002
1.95,0,0
2,0.002,0.002
2.05,0.002,0.002
2.1,0,0.002
2.15,0,0.001
2.2,0,0.001
2.25,0,0.003
2.3,0,0
2.35,0,0
2.4,0,0
2.45,0,0.001
2.5,0,0.001
2.55,0,0.001
\end{filecontents}


\usetikzlibrary{datavisualization}


    \usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\centering
    \begin{axis}[
        axis lines*=left,
        axis background/.style={fill=gray!10},
        width=12cm,
        height=8cm,
        x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
        y label style={at={(axis description cs:-0.05,.5)},anchor=south},
        ylabel= Density,
        xlabel= Absolute percentage errors for SBP and GICS,
        axis on top,
        xmin=0,
        xmax=2,
        ymin=-0.017,
        ymax=2.5,
        domain=xmin:xmax
    ]
        \path[name path=axis] (axis cs:xmin,0) -- (axis cs:xmax,0);
        \addplot[name path=table, fill=MyBlue!45, opacity=0.7] table [ x index=0, y index=1, col sep=comma, ] {data_ape_dist.dat};
        \addplot[name path=table, fill=red!45, opacity=0.6] table [ x index=0, y index=2, col sep=comma, ] {data_ape_dist.dat};
        fill between [of=axis and table];
    \end{axis}
\end{tikzpicture}



\end{document}

我尝试绘制背景矩形,或指定[frame background rectangle]之后\begin{tikzpicture},但没有成功。有人能给我指出正确的方向吗?谢谢!

答案1

您的代码存在许多问题,无法编译。下面的代码可以编译并生成您要求的结果。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{filecontents}
\usetikzlibrary{positioning,shapes}
\begin{filecontents}{data_ape_dist.dat}
x,y,y2
0,0,0
0.05,2.021,1.781
0.1,1.928,1.734
0.15,1.798,1.634
0.2,1.567,1.45
0.25,1.316,1.274
0.3,1.167,1.177
0.35,0.948,0.993
0.4,0.744,0.869
0.45,0.652,0.718
0.5,0.492,0.57
0.55,0.428,0.465
0.6,0.304,0.389
0.65,0.256,0.272
0.7,0.203,0.229
0.75,0.133,0.178
0.8,0.092,0.141
0.85,0.058,0.091
0.9,0.035,0.063
0.95,0.036,0.051
1,0.021,0.043
1.05,0.012,0.026
1.1,0.014,0.031
1.15,0.011,0.018
1.2,0.01,0.012
1.25,0.012,0.022
1.3,0.012,0.012
1.35,0.006,0.006
1.4,0.013,0.005
1.45,0.009,0.008
1.5,0.003,0.006
1.55,0.004,0.007
1.6,0.001,0.009
1.65,0.001,0.002
1.7,0.002,0.003
1.75,0,0.003
1.8,0.001,0.005
1.85,0.001,0.002
1.9,0,0.002
1.95,0,0
2,0.002,0.002
2.05,0.002,0.002
2.1,0,0.002
2.15,0,0.001
2.2,0,0.001
2.25,0,0.003
2.3,0,0
2.35,0,0
2.4,0,0
2.45,0,0.001
2.5,0,0.001
2.55,0,0.001
\end{filecontents}


\usetikzlibrary{datavisualization}


    \usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\centering
    \begin{axis}[
    %    axis lines*=left,
        axis background/.style={fill=gray!10},
        width=12cm,
        height=8cm,
        x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
        y label style={at={(axis description cs:-0.05,.5)},anchor=south},
        ylabel= Density,
        xlabel= Absolute percentage errors for SBP and GICS,
        axis on top,
        xmin=0,
        xmax=2,
        ymin=-0.017,
        ymax=2.5,
        domain=0:2
    ]
        \path[name path=axis] (axis cs:0,0) -- (axis cs:2,0);
        \addplot[name path=table, fill=blue!45, opacity=0.7] table [ x index=0, y index=1, col sep=comma, ] {data_ape_dist.dat};
        \addplot[name path=table, fill=red!45, opacity=0.6] table [ x index=0, y index=2, col sep=comma, ] {data_ape_dist.dat};
        fill between [of=axis and table];
    \end{axis}
\end{tikzpicture}



\end{document}

相关内容