如何创建堆叠条形图

如何创建堆叠条形图

我想创建一个条形图,就像下面这张取自论文的图片一样, 在此处输入图片描述

我也看到了这个问题我如何将“ybar”和“ybar stacked”与 pgfplots 混合?但由于紧急情况,它对我来说不起作用,并且多次无法生成具有如上图所示设置的图形。

在此先感谢大家对这一重要请求的帮助和协助。

另外,这是我的原始代码,

\documentclass{article}
%\usepackage{pgfplots}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{caption}
\usepackage{polyglossia}
\pagenumbering{gobble}
\makeatletter
\newcommand\resetstackedplots{
\makeatletter
\pgfplots@stacked@isfirstplottrue
\makeatother
\addplot [forget plot,draw=none] coordinates{(-9,0) (-5,0) (-1,0) (3,0) (7,0) (12,0)};
}
\makeatother
\begin{document}
\begin{figure}[h]
\makeatletter
%\pgfplotsset{
%    calculate offset/.code={
%        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
%        \pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
%        \pgfkeys{/pgf/fpu=false}
%    },
%    every node near coord/.style={
%        /pgfplots/calculate offset,
%        yshift=-\testmacro
%    }
%}
\centering
\begin{tikzpicture}
\begin{axis}[
    width = 12cm,
    height= 10cm,
    ybar stacked,
    xtick=data,
    bar width=0.15cm,
    x = 0.55cm,
    ymin=0,
    enlarge x limits={true, abs value=0.4},
    xticklabels={Football, PolBlogs, PolBooks, Facebook,CalTech,Rice},
%    x t style={below=25mm},
    xmin = -11,
    xmax = 13,
    xmajorgrids = true,
    ymajorgrids = true,
    legend entries={NMI,f1-score},
%    legend style={
%            at={(-0.05,.65)},
%            anchor=south east,
%            column sep=1ex
%        },
         xticklabel style={yshift=-5ex},
         % visualization depends on=y \as \rawy,
           % symbolic x coords={0,1,2,3}, enlarge x limits=0.15,
%            every node near coord/.append style={
%                    font=\tiny,
%                    shift={(axis direction cs:0,-\rawy)}
%                },
]
\addplot +[bar shift=-0.9 cm,color = blue!100] coordinates {(-9,.62) (-5,.12) (-1,.21) (3,.17) (7,.1) (12,0.01)};
\addplot +[bar shift=-0.9 cm,color = red!100] coordinates {(-9,.76) (-5,.77) (-1,.74) (3,.35) (7,.37) (12, .15)};
%\addplot +[bar shift=-.2cm] coordinates {(1,0.75) (2,.5) (3,.5)};
\resetstackedplots
\addplot  +[bar shift=-.6cm,color = blue!100]coordinates {(-9,0.13) (-5,0.01) (-1,0.01) (3,0.04) (7,0.01) (12,.02)};
\addplot  +[bar shift=-.6cm,color = red!100] coordinates {(-9,0.4) (-5,.44) (-1,.31) (3,.11) (7,.13) (12,0)};
%\addplot  +[bar shift=.2cm] coordinates {(1,0.2) (2,0.1) (3,0.3)};
\resetstackedplots
\addplot  +[bar shift=-.3cm,color = blue!100]coordinates {(-9,.01) (-5,0.001) (-1,0.04) (3,.05) (7,.01) (12,.01)};
\addplot  +[bar shift=-.3cm,color = red!100] coordinates {(-9,0.2) (-5,0.001) (-1,.55) (3,.14) (7,.13) (12,0)};
%\addplot  +[bar shift=-.4cm] coordinates {(1,0.2) (2,0.1) (3,0.3)};
\resetstackedplots
\addplot  +[bar shift=0cm,color = blue!100]coordinates {(-9,.45) (-5,.24) (-1,0.43) (3,.11) (7,.21) (12,.14)};
\addplot  +[bar shift=0cm,color = red!100] coordinates {(-9,0.84) (-5,.72) (-1,0.69) (3,.24) (7,.52) (12,.36)};
\resetstackedplots
\addplot  +[bar shift=.3cm,color = blue!100]coordinates {(-9,.56) (-5,.01) (-1,0.01) (3,.01) (7,.05) (12,.25)};
\addplot  +[bar shift=.3cm,color = red!100] coordinates {(-9,0.73) (-5,.3) (-1,0.35) (3,.01) (7,.29) (12,.42)};
\resetstackedplots
\addplot  +[bar shift=.6cm,color = blue!100]coordinates {(-9,.21) (-5,.21) (-1,.49) (3,.01) (7,.01) (12,0.01)};
\addplot  +[bar shift=.6cm,color = red!100]coordinates {(-9,.76) (-5,.66) (-1,.73) (3,.12) (7,.03) (12,.1)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

答案1

嗯,不幸的是这不是一个理想的解决方案,因为显然pgfplots不能混合ybarybar stacked属性,因此出现在同一坐标处的条形图x将不可避免地重叠。

我没有找到任何可以改变此行为的方法(无论是在本网站还是在手册中)。也许其他人知道如何自动执行此操作,但到目前为止我还不知道该怎么做。

输出

在此处输入图片描述

代码

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots,pgfplotstable}

\pgfplotsset{compat=1.13}

\definecolor{dgreen}{RGB}{0,83,54}
\definecolor{lgreen}{RGB}{0,255,28}

\pgfplotscreateplotcyclelist{cyclecol}{
{fill=dgreen},
{fill=lgreen},
}

\pgfplotstableread[col sep=comma]{
index, first, second
 1,     0.62,     .76
 2,     0.12,     .77
 3,     0.21,     .74
 4,     0.17,     .35
 5,     0.1,      .37
 6,     0.01,     .15
% 
 8,     0.13,    0.4  
 9,     0.01,     .44  
10,     0.01,     .31  
11,     0.04,     .11  
12,     0.01,     .13  
13,     0.02,    0 
%
15,      .01,    0.2
16,     0.001,   0.001
17,     0.04,     .55
18,      .05,     .14
19,      .01,     .13
20,      .01,    0 
%      
22,      .45,    0.84
23,      .24,     .72
24,     0.43,    0.69
25,      .11,     .24
26,      .21,     .52
27,      .14,     .36
%      
29,      .56,    0.73
30,      .01,     .3
31,     0.01,    0.35
32,      .01,     .01
33,      .05,     .29
34,      .25,     .42
%      
36,      .21,     .76
37,      .21,     .66
38,      .49,     .73
39,      .01,     .12
40,      .01,     .03
41,     0.01,     .1
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    %width=12cm, height=10cm,
    axis lines*=left, ymajorgrids,
    ymin=0, xmin=0,
    x=3mm, y=5cm,
    %ybar=1mm,
    %bar shift=1mm,
    xtick={3.5,10.5,17.5,24.5,31.5,38.5},
    bar width=1.5mm,
    ytick={0,1},
    enlarge x limits={true, abs value=0.4},
    xticklabels={Football,PolBlogs,PolBooks,Facebook,CalTech,Rice}, 
    legend entries={NMI,f1-score},
    xticklabel style={yshift=-1ex},
    cycle list name=cyclecol
]

\addplot table[x=index, y=first, col sep=comma] \datatable;
\addplot table[x=index, y=second, col sep=comma] \datatable;

\end{axis}
\end{tikzpicture}
\end{document}

相关内容