须状图/箱线图,如何绘制?

须状图/箱线图,如何绘制?

我制作了图形(代码如下),但我的修订者要求绘制箱线图。我承认我不知道该怎么做。

我的数据库

Author min avg max mediana deviation
ClercA 100.1900 131.5194 180.2100 130.2000 16.0537
ClercB 100.1900 131.9290 190.2000 130.2000 15.6576
ClercC 100.1900 145.2007 210.2000 140.2100 17.3981
ClercD 100.1900 113.8061 150.2100 110.2000 10.8342
ClercE 100.1900 131.7290 180.2000 130.2000 15.7357
EberhartA 100.1900 129.8890 170.2100 130.2000 15.8132
EberhartB 100.1900 127.4685 170.2000 130.1900 14.4167
EberhartC 100.2000 142.6508 190.2100 140.2000 16.6241
EberhartD 100.1800 113.9665 150.2100 110.2000 11.2164
EberhartE 100.1900 129.6588 190.2100 130.2000 15.0647
XinA 100.1900 126.8997 170.2000 130.2000 14.3637
XinB 100.1900 124.3386 170.2000 130.1900 13.5221
XinC 100.1900 137.3308 190.2000 130.2100 16.0478
XinD 100.1800 113.8771 160.2000 110.2000 11.1794
XinE 100.1800 126.7993 180.2100 130.1900 14.4938

我的乳胶代码

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{adjustbox}
\pagestyle{empty}
\usetikzlibrary{positioning, backgrounds}

\begin{document}


    \begin{figure}[h!]%{width=\textwidth}
    \begin{adjustbox}{width=\textwidth}
        \centering
        \rotatebox{90}{%
            \begin{tikzpicture}[framed]
            \begin{axis}[
            xbar stacked, 
            xlabel={fitness},
            legend style={at={(0.5,-0.05)},anchor=north,legend columns=-1},
            symbolic y coords={ClercA,ClercB,ClercC,ClercD,ClercE,
                EberhartA, EberhartB, EberhartC, EberhartD, EberhartE,
                XinA, XinB, XinC, XinD, XinE},
            ytick=data,
            %enlarge y limits={abs=.70cm},
            %bar width={0.15cm},
            y=.45cm,
            y tick label style={rotate=-45,anchor=east,scale=0.5,font=\bfseries},
            %yticklabel style={\textbf{\pgfplotsretval},
            %every node near coord/.append style={anchor=south},
            nodes near coords, nodes near coords align={vertical},
           nodes near coords style={scale=0.35,font=\bfseries,text=black,rotate=-90}]


            %% mínimo
            \addplot+[xbar, violet!40] coordinates 
            {(100.1900,ClercA) (100.1900,ClercB) (100.1900,ClercC) (100.1800,ClercD)(100.1900,ClercE)

                (100.1900,EberhartA) (100.1900,EberhartB) (100.2000,EberhartC) (100.1800,EberhartD) (100.1900,EberhartE)

                (100.1900,XinA) (100.1900,XinB) (100.1900,XinC) (100.1800,XinD) (100.1800,XinE) }; %min

            %% average
            \addplot+[xbar, red!70] coordinates 
            {(131.5194,ClercA) (131.9290,ClercB) (145.2007,ClercC) (113.8061,ClercD) (131.7290,ClercE)

                (129.8890,EberhartA) (127.4685,EberhartB) (142.6508,EberhartC) (113.9665,EberhartD) (129.6588,EberhartE)

                (126.8997,XinA) (124.3386,XinB) (137.3308,XinC) (113.8771,XinD) (126.7993,XinE) }; %avg

            %% máximo
            \addplot+[xbar, green!50] coordinates 
            {(180.2100,ClercA) (190.2000,ClercB) (210.2000,ClercC) (150.2100,ClercD) (180.2000,ClercE)

                (170.2100,EberhartA) (170.2000,EberhartB) (190.2100,EberhartC) (150.2100,EberhartD) (190.2100,EberhartE)

                (170.2000,XinA) (170.2000,XinB) (190.2000,XinC) (160.2000,XinD) (180.2100,XinE) }; %max

            %% standart deviation
            \addplot+[xbar,nodes near coords, nodes near coords align={center}, purple!40] coordinates 
            {(16.0537,ClercA) (15.6576,ClercB) (17.3981,ClercC) (10.8342,ClercD)(15.7357,ClercE)

                (15.8132,EberhartA) (14.4167,EberhartB) (16.6241,EberhartC) (11.2164,EberhartD) (15.0647,EberhartE)

                (14.3637,XinA) (13.5221,XinB) (16.0478,XinC) (11.1794,XinD) (14.4938,XinE) }; %deviation

            %%mediana
            \addplot+[xbar, blue!40] coordinates 
            {(130.2000,ClercA) (130.2000,ClercB) (140.2100,ClercC) (110.2000,ClercD)(130.2000,ClercE)

                (130.2000,EberhartA) (130.1900,EberhartB) (140.2000,EberhartC) (110.2000,EberhartD) (130.2000,EberhartE)

                (130.2000,XinA) (130.1900,XinB) (130.2100,XinC) (110.2000,XinD) (130.1900,XinE) }; %mediana



            \legend{min,avg,max, deviation, mediana}    

            \end{axis}
            \end{tikzpicture}}
    \end{adjustbox}
    \caption{Scenario settings: a) $c_1 = 1$ and $c_2 = 2$; b) $c_1 = 2$ and $c_2 = 1$;
        c) $c_l = c_2 = 1$; d) $c_1 = c_2 = 2$ and e) $c_1 = c_2 = 1.496$.}
    \label{fig:simulation_results_fitness_gpop100}
    %\end{subfigure}%
\end{figure}


\begin{figure}[h!]
    \begin{adjustbox}{width=\textwidth}

            \begin{tikzpicture}[framed]
            \begin{axis}[
            xbar stacked, 
            xlabel={fitness},
            legend style={at={(0.5,-0.05)},anchor=north,legend columns=-1},
            symbolic y coords={ClercA,ClercB,ClercC,ClercD,ClercE,
                EberhartA, EberhartB, EberhartC, EberhartD, EberhartE,
                XinA, XinB, XinC, XinD, XinE},
            ytick=data,
            %enlarge y limits={abs=.70cm},
            %bar width={0.15cm},
            y=.45cm,
            y tick label style={anchor=east,scale=0.5,font=\bfseries},
            %every node near coord/.append style={anchor=center},
            nodes near coords,  nodes near coords style={scale=0.35,text=black,rotate=-90},]


            %% mínimo
            \addplot+[xbar, nodes near coords style={anchor=south},violet!40] coordinates 
            {(100.1900,ClercA) (100.1900,ClercB) (100.1900,ClercC) (100.1800,ClercD)(100.1900,ClercE)

                (100.1900,EberhartA) (100.1900,EberhartB) (100.2000,EberhartC) (100.1800,EberhartD) (100.1900,EberhartE)

                (100.1900,XinA) (100.1900,XinB) (100.1900,XinC) (100.1800,XinD) (100.1800,XinE) } ; %min

            %% average
            \addplot+[xbar,red!70] coordinates 
            {(131.5194,ClercA) (131.9290,ClercB) (145.2007,ClercC) (113.8061,ClercD) (131.7290,ClercE)

                (129.8890,EberhartA) (127.4685,EberhartB) (142.6508,EberhartC) (113.9665,EberhartD) (129.6588,EberhartE)

                (126.8997,XinA) (124.3386,XinB) (137.3308,XinC) (113.8771,XinD) (126.7993,XinE) }; %avg

            %% máximo
            \addplot+[xbar,green!50] coordinates 
            {(180.2100,ClercA) (190.2000,ClercB) (210.2000,ClercC) (150.2100,ClercD) (180.2000,ClercE)

                (170.2100,EberhartA) (170.2000,EberhartB) (190.2100,EberhartC) (150.2100,EberhartD) (190.2100,EberhartE)

                (170.2000,XinA) (170.2000,XinB) (190.2000,XinC) (160.2000,XinD) (180.2100,XinE) }; %max

            %% standart deviation
            \addplot+[xbar,purple!40] coordinates 
            {(16.0537,ClercA) (15.6576,ClercB) (17.3981,ClercC) (10.8342,ClercD)(15.7357,ClercE)

                (15.8132,EberhartA) (14.4167,EberhartB) (16.6241,EberhartC) (11.2164,EberhartD) (15.0647,EberhartE)

                (14.3637,XinA) (13.5221,XinB) (16.0478,XinC) (11.1794,XinD) (14.4938,XinE) }; %deviation

            %%mediana
            \addplot+[xbar,blue!40] coordinates 
            {(130.2000,ClercA) (130.2000,ClercB) (140.2100,ClercC) (110.2000,ClercD)(130.2000,ClercE)

                (130.2000,EberhartA) (130.1900,EberhartB) (140.2000,EberhartC) (110.2000,EberhartD) (130.2000,EberhartE)

                (130.2000,XinA) (130.1900,XinB) (130.2100,XinC) (110.2000,XinD) (130.1900,XinE) }; %mediana



            \legend{min,avg,max, deviation, mediana}    

            \end{axis}
            \end{tikzpicture}
    \end{adjustbox}
    \caption{Scenario settings: a) $c_1 = 1$ and $c_2 = 2$; b) $c_1 = 2$ and $c_2 = 1$;
        c) $c_l = c_2 = 1$; d) $c_1 = c_2 = 2$ and e) $c_1 = c_2 = 1.496$.}
    \label{fig:simulation_results_fitness_gpop100}
    %\end{subfigure}%
\end{figure}

\end{document}

答案1

这是将您的表格转换为一些箱线图。(不幸的是,我无法申请这个很好的答案就您的情况而言。)它不会与您自己的情节的出色外观相竞争。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{statistics}
\pgfplotstableread{%
Author min avg max mediana deviation
ClercA 100.1900 131.5194 180.2100 130.2000 16.0537
ClercB 100.1900 131.9290 190.2000 130.2000 15.6576
ClercC 100.1900 145.2007 210.2000 140.2100 17.3981
ClercD 100.1900 113.8061 150.2100 110.2000 10.8342
ClercE 100.1900 131.7290 180.2000 130.2000 15.7357
EberhartA 100.1900 129.8890 170.2100 130.2000 15.8132
EberhartB 100.1900 127.4685 170.2000 130.1900 14.4167
EberhartC 100.2000 142.6508 190.2100 140.2000 16.6241
EberhartD 100.1800 113.9665 150.2100 110.2000 11.2164
EberhartE 100.1900 129.6588 190.2100 130.2000 15.0647
XinA 100.1900 126.8997 170.2000 130.2000 14.3637
XinB 100.1900 124.3386 170.2000 130.1900 13.5221
XinC 100.1900 137.3308 190.2000 130.2100 16.0478
XinD 100.1800 113.8771 160.2000 110.2000 11.1794
XinE 100.1800 126.7993 180.2100 130.1900 14.4938
}\datatable

\begin{document}
\begin{tikzpicture}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\rownumber}{\pgfplotsretval-1}
\begin{axis}[boxplot/draw direction=x,yticklabels={ClercA,ClercB,ClercC,ClercD,ClercE,
                EberhartA, EberhartB, EberhartC, EberhartD, EberhartE,
                XinA, XinB, XinC, XinD, XinE},ytick={1,...,\the\numexpr\rownumber+1},height=12cm
                ]
\typeout{\rownumber}
\pgfplotsinvokeforeach{0,...,\rownumber}{
 \pgfplotstablegetelem{#1}{min}\of\datatable
 \edef\mymin{\pgfplotsretval}
 \pgfplotstablegetelem{#1}{avg}\of\datatable
 \edef\myavg{\pgfplotsretval}
 \pgfplotstablegetelem{#1}{max}\of\datatable
 \edef\mymax{\pgfplotsretval}
 \pgfplotstablegetelem{#1}{mediana}\of\datatable
 \edef\mymediana{\pgfplotsretval}
 \pgfplotstablegetelem{#1}{deviation}\of\datatable
 \edef\mydeviation{\pgfplotsretval}
 \typeout{\mymin,\mymax,\myavg,\mymediana,\mydeviation}
 \pgfmathsetmacro{\mylowerq}{\mymediana-\mydeviation}
 \pgfmathsetmacro{\myupperq}{\mymediana+\mydeviation}
 \edef\temp{\noexpand\addplot+[
    boxplot prepared={
     lower whisker=\mymin,
     upper whisker=\mymax,
     lower quartile=\mylowerq,
     upper quartile=\myupperq,
     median=\myavg
  }
  ]coordinates {};}
 \temp
}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

经过一番尝试,我得到了主管想要的结果!@marmot,再次感谢!

\documentclass[crop=false]{standalone}
\usepackage{pgfplotstable}
\usepgfplotslibrary{colorbrewer}
%\pgfplotsset{compat=1.16}
\usepgfplotslibrary{statistics}
\pgfplotstableread{%
Author min avg max mediana deviation
ClercA 100.1900 131.5194 180.2100 130.2000 16.0537
ClercB 100.1900 131.9290 190.2000 130.2000 15.6576
ClercC 100.1900 145.2007 210.2000 140.2100 17.3981
ClercD 100.1900 113.8061 150.2100 110.2000 10.8342
ClercE 100.1900 131.7290 180.2000 130.2000 15.7357
EberhartA 100.1900 129.8890 170.2100 130.2000 15.8132
EberhartB 100.1900 127.4685 170.2000 130.1900 14.4167
EberhartC 100.2000 142.6508 190.2100 140.2000 16.6241
EberhartD 100.1800 113.9665 150.2100 110.2000 11.2164
EberhartE 100.1900 129.6588 190.2100 130.2000 15.0647
XinA 100.1900 126.8997 170.2000 130.2000 14.3637
XinB 100.1900 124.3386 170.2000 130.1900 13.5221
XinC 100.1900 137.3308 190.2000 130.2100 16.0478
XinD 100.1800 113.8771 160.2000 110.2000 11.1794
XinE 100.1800 126.7993 180.2100 130.1900 14.4938
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=y,
xticklabels={ClercA,ClercB,ClercC,ClercD,ClercE,EberhartA, EberhartB, EberhartC, EberhartD, EberhartE,XinA, XinB, XinC, XinD, XinE},
xtick={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
x tick label style={scale=0.5,font=\bfseries, rotate=60,,align=center},
ylabel={fitness},cycle list/YlGnBu-5 ]


\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\rownumber}{\pgfplotsretval-1}
\pgfplotsinvokeforeach{0,...,\rownumber}{

    \pgfplotstablegetelem{#1}{min}\of\datatable
    \edef\mymin{\pgfplotsretval}

    \pgfplotstablegetelem{#1}{avg}\of\datatable
    \edef\myavg{\pgfplotsretval}

    \pgfplotstablegetelem{#1}{max}\of\datatable
    \edef\mymax{\pgfplotsretval}

    \pgfplotstablegetelem{#1}{mediana}\of\datatable
    \edef\mymediana{\pgfplotsretval}

    \pgfplotstablegetelem{#1}{deviation}\of\datatable
    \edef\mydeviation{\pgfplotsretval}

    \typeout{\mymin,\mymax,\myavg,\mymediana,\mydeviation}
    \pgfmathsetmacro{\mylowerq}{\mymediana-\mydeviation}
    \pgfmathsetmacro{\myupperq}{\mymediana+\mydeviation}
    \edef\temp{\noexpand\addplot+[,
        boxplot prepared={
            lower whisker=\mymin,
            upper whisker=\mymax,
            lower quartile=\mylowerq,
            upper quartile=\myupperq,
            median=\myavg,
            every box/.style={solid,fill,opacity=0.5},
            every whisker/.style={solid },
            every median/.style={solid},
        }, 
        ]coordinates {};}
    \temp
}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\rownumber}{\pgfplotsretval-1}
\begin{axis}[boxplot/draw direction=y,
xticklabels={ClercA,ClercB,ClercC,ClercD,ClercE,EberhartA, EberhartB, EberhartC, EberhartD, EberhartE,XinA, XinB, XinC, XinD, XinE},
xtick={1,...,\the\numexpr\rownumber+1},
x tick label style={scale=0.5,font=\bfseries, rotate=60,align=center},
ylabel={fitness},cycle list/Set1-5 ]


\pgfplotsinvokeforeach{0,...,\rownumber}{

    \pgfplotstablegetelem{#1}{min}\of\datatable
    \edef\mymin{\pgfplotsretval}

    \pgfplotstablegetelem{#1}{avg}\of\datatable
    \edef\myavg{\pgfplotsretval}

    \pgfplotstablegetelem{#1}{max}\of\datatable
    \edef\mymax{\pgfplotsretval}

    \pgfplotstablegetelem{#1}{mediana}\of\datatable
    \edef\mymediana{\pgfplotsretval}

    \pgfplotstablegetelem{#1}{deviation}\of\datatable
    \edef\mydeviation{\pgfplotsretval}

    \typeout{\mymin,\mymax,\myavg,\mymediana,\mydeviation}
    \pgfmathsetmacro{\mylowerq}{\mymediana-\mydeviation}
    \pgfmathsetmacro{\myupperq}{\mymediana+\mydeviation}
    \edef\temp{\noexpand\addplot+[,
        boxplot prepared={
            lower whisker=\mymin,
            upper whisker=\mymax,
            lower quartile=\mylowerq,
            upper quartile=\myupperq,
            median=\myavg,
            every box/.style={solid,fill,opacity=0.5},
            every whisker/.style={solid },
            every median/.style={solid},
        }, 
        ]coordinates {};}
    \temp
}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容