填充图表至 x 轴以使其看起来像图像

填充图表至 x 轴以使其看起来像图像

我需要一些帮助,使我的图表看起来像这样(如果可能的话):

enter image description here

将两种音调颜色放在 x 轴上。

这是我目前拥有的代码:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{
        fit,
        shapes,
        pgfplots.groupplots,
    }
\pgfplotsset{compat=newest}
\begin{filecontents*}{data_1.txt}
TypeII_Error Rate
0, 2.1
1, 12.5
2, 13.5
3, 18.5
4, 22.5
\end{filecontents*}

\begin{document}


\centering
\begin{tikzpicture}
\begin{axis}[width=14cm,height=7cm, title = {\emph{Variation of Type II Error vs. Misclassification Rates}},
                xlabel={\emph{Type II Error}},
                ylabel={\emph{Missclassification Rate (\%)}},
                ymax=30,
                ymin=0,
                grid=both,
                every major grid/.style={gray, opacity=0.5}]

\addplot [mark=*,red,mark options={scale=.65}] table[x index=0,y index=1,col sep=comma] {data_1.txt};
\end{axis}
\end{tikzpicture}


\end{document} 

答案1

所以您的意思是与以下内容类似的东西?

有关其工作原理的详细信息,请查看代码中的注释。

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{
        pgfplots.fillbetween,
    }
    \pgfplotsset{
        compat=1.14,
        % define a style for the nodes at the beginning and end of the plot lines
        /tikz/Node/.style={
            circle,
            draw=white,
        },
        % define a style for the labels on the nodes at the beginning and end
        % of the plot lines
        /tikz/Label/.style={
            fill=white,
            fill opacity=0.5,
            text opacity=1,
        },
    }
\begin{filecontents*}{data_1.txt}
x, y1,   y2
0, 2.1,  7.0
1, 12.5, 16.0
2, 13.5, 25.4
3, 18.5, 22.8
4, 22.5, 28.0
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
        % define some colors for later use
        \colorlet{dark orange}{orange!50!black}
        \colorlet{light orange}{orange!50}
        \colorlet{lighter orange}{orange!25}
        \colorlet{dark green}{green!75!black}
        \colorlet{light green}{green!50}
        \colorlet{lighter green}{green!25}
    \begin{axis}[
        % removed `\emph' from `title' and `labels'
        title={Variation of Type II Error vs. Misclassification Rates},
        xlabel={Type II Error},
        ylabel={Missclassification Rate (\%)},
        % added `xmin' and `xmax'
        xmin=0,
        xmax=4,
        ymax=30,
        ymin=0,
        % just show the `xticks' at data points
        xtick=data,
        grid=both,
        % the lines should be `smooth' ...
        smooth,
        % ... and should not show any markers
        no markers,
        % so the nodes at the beginning and end of the lines are not clipped
        clip=false,
        % common `table' options
        table/x=x,
        table/col sep=comma,
    ]
        % path for the lower `fill between' limit
        \path [name path=origin] (\pgfkeysvalueof{/pgfplots/xmin},0)
            -- (\pgfkeysvalueof{/pgfplots/xmax},0);

        % add the plot ...
        \addplot [very thick,dark orange,name path=y1] table [y=y1] {data_1.txt}
            % ... and add the nodes (using the defined style)
            % at the beginning and end of the plot ...
            node [Node,fill=dark orange,at start,
                % ... which should also be labeled (using the defined style)
                % with the current y value
                label={[Label]right:%
                    \pgfplotspointplotattime{0}%
                    \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
                },
            ] {}
            node [Node,fill=dark orange,at end,
                label={[Label]left:%
                    \pgfplotspointplotattime{1}%
                    \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}%
                },
            ] {}
        ;
        % same again for the second line
        \addplot [very thick,dark green,name path=y2] table [y=y2] {data_1.txt}
            node [Node,fill=dark green,at start,
                label={[Label]right:%
                     \pgfplotspointplotattime{0}%
                     \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
                },
            ] {}
            node [Node,fill=dark green,at end,
                label={[Label]left:%
                     \pgfplotspointplotattime{1}%
                     \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}
                },
            ] {}
        ;

        % now fill the areas under or in between the plots respectively using
        % a loop (for somplicity)
        \pgfplotsinvokeforeach {0,2} {
            \addplot [light orange,opacity=0.5] fill between [
                of=origin and y1,
                soft clip={domain=#1:#1+1},
            ];
            \addplot [light green,opacity=0.5] fill between [
                of=y1 and y2,
                soft clip={domain=#1:#1+1},
            ];
        }
        \pgfplotsinvokeforeach {1,3} {
            \addplot [lighter orange,opacity=0.5] fill between [
                of=origin and y1,
                soft clip={domain=#1:#1+1},
            ];
            \addplot [lighter green,opacity=0.5] fill between [
                of=y1 and y2,
                soft clip={domain=#1:#1+1},
            ];
        }
\end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

答案2

您的 MWE 和提供的图像不同,因此不清楚您是否喜欢两条堆叠曲线或只喜欢一条堆叠曲线,如下图所示:

enter image description here

\RequirePackage{filecontents}
\begin{filecontents*}{data_1.txt}
TypeII_Error Rate
0, 2.1
1, 12.5
2, 13.5
3, 18.5
4, 22.5
\end{filecontents*}

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}

\begin{document}
\centering
    \begin{tikzpicture}
\begin{axis}[width=14cm,height=7cm, 
    title ={\emph{Variation of Type II Error vs. Misclassification Rates}},
    xlabel={\emph{Type II Error}},
    ylabel={\emph{Missclassification Rate} (\%)},%    
    ymin=0, ymax=30,
    xmin=0, xmax=4,
    grid=both,
    every major grid/.style={gray, opacity=0.5},
%        
    mark options={mark=o,scale=.65}
            ]
\addplot +[name path=A,red,ultra thick] 
        table[x index=0,y index=1,col sep=comma] {data_1.txt};
\path[name path=B] (0,0) -- (4,0);
\addplot[orange!30,semitransparent] fill between[of=A and B];
\end{axis}
    \end{tikzpicture}
\end{document}

如果有两条曲线,请将第二条曲线的坐标添加到表中。

编辑:使用宏的代码更简单\closedcycle,但是在这种情况下,填充区域会遇到以下行:

enter image description here

\RequirePackage{filecontents}
\begin{filecontents*}{data_1.txt}
TypeII_Error Rate % added third column
0, 2.1  ,   7
1, 12.5 ,   2
2, 13.5 ,   12
3, 18.5 ,   1
4, 22.5 ,   1
\end{filecontents*}

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}

\begin{document}
\centering
    \begin{tikzpicture}
\begin{axis}[width=14cm,height=7cm,
%    stack plots=y, 
    title ={\emph{Variation of Type II Error vs. Misclassification Rates}},
    xlabel={\emph{Type II Error}},
    ylabel={\emph{Missclassification Rate} (\%)},%    
    ymin=0, ymax=30,
    xmin=0, xmax=4,
    grid=both,
    every major grid/.style={gray, opacity=0.5},
%        
    mark options={mark=o,scale=.65}
            ]
\addplot +[red,ultra thick,fill=red!30, semitransparent] 
        table[x index=0,y index=1,col sep=comma] {data_1.txt} \closedcycle;
\addplot +[blue,ultra thick,fill=blue!30, semitransparent]
        table[x index=0,y index=2,col sep=comma] {data_1.txt} \closedcycle;
\end{axis}
    \end{tikzpicture}
\end{document}

相关内容