强制将堆叠条形图中的最后两个条形向下

强制将堆叠条形图中的最后两个条形向下

我想使用 ShareLaTeX 中的 tikz 以堆叠条形图来说明数据。我正在处理两个文件:一个名为“按类型划分的众筹价值.txt”的数据文件和我正在处理的文档“Crowdfunding.tex”。我有包含前三年不同类型众筹的数据,但只有过去两年的总价值。txt 文件中的数据如下所示:

year Donation Equity Lending Reward Mixed-others Total

2010 460.4 49.9 316.5 15.7 0 
2011 675.7 88.9 554.9 61.5 0
2012 979.4 115.7 1169.7 383.3 2.4
2013 0 0 0 0 0 6100
2014 0 0 0 0 0 16200

我在其中用 0 来表示不同类型。(还尝试用“。”替换 0 来强制最后两个条形,但没有成功。目前输出如下所示:

enter image description here

我想要完成四件事:1)强制将两个黄色条形图设置为 0 美元十亿 = 02)将标签展开到一行3)移除上部和顶部轴(因此只保留标记的轴4)在最后两个条形图内插入标签(2013 年的条形图为 6,2,2014 年的条形图为 16,2)。

这是我的代码:

    %\begin{comment}
 \begin{figure}[H] 
    \centering
    \begin{tikzpicture} 
        \begin{axis}[
            legend style={
    legend columns=4,
        at={(xticklabel cs:0.5)},
        anchor=north,
        draw=none
    },
            ybar stacked,
            xlabel= Year,
            ylabel = USD bn,
            %ymajorgrids = true,
            width =.90\textwidth,
            height = 1.3\textwidth,
            %xmin = 2005,
            %xmax = 2012,
            ymin = -6,
            ymax = 10000,
            ytick={0,1000,...,15000},
            % axis x line* = bottom,
            % axis y line* = left,
            xticklabels = none,
            extra x ticks = {2010, 2011, 2012, 2013, 2014},
            extra x tick labels = {2010, 2011, 2012, 2013, 2014},
            legend style={font=\footnotesize},
            %area legend,
            %scaled ticks = false,
            %y tick label style = {/pgf/number format/use comma}
            ]
            \addplot+[mark=none, fill=dbscan, draw = dbscan, bar width = 9mm] table[x=year, y=Donation] {Chapters/Chapter_2/Value of Crowdfunding by type.txt};
            \addplot+[mark=none, fill=red, draw = red, bar width = 9mm] table[x=year, y=Equity] {Chapters/Chapter_2/Value of Crowdfunding by type.txt};
            \addplot+[mark=none, fill=orange, draw = orange, bar width = 9mm] table[x=year, y=Lending] {Chapters/Chapter_2/Value of Crowdfunding by type.txt};
            \addplot+[mark=none, fill=black, draw = black, bar width = 9mm] table[x=year, y=Reward] {Chapters/Chapter_2/Value of Crowdfunding by type.txt};
            \addplot+[mark=none, fill=blue, draw = blue, bar width = 9mm] table[x=year, y=Mixed-others] {Chapters/Chapter_2/Value of Crowdfunding by type.txt};
            \addplot+[mark=none, fill=yellow, draw = yellow, bar width = 9mm] table[x=year, y=Total] {Chapters/Chapter_2/Value of Crowdfunding by type.txt};
            \legend{Donation, Equity, Lending, Reward, Mixed other, Total}
            %\coordinate (2013) at (8mm, 100);
        \end{axis} 
        %\node at (2013) {XY};
    \end{tikzpicture}
    \caption{Testing}
    \label{Testing label}
    \floatfoot{\textit{Source}: Testing source}
\end{figure}

我被困住了!任何帮助我都感激不尽。谢谢。

答案1

您收到一条错误消息,提示某些行缺少列。如果我0在 2011、2012 和 2013 年的最后一列(总计)中添加,黄色条将从 bn=0 开始。

如果图例只有一行,请使用

legend columns=-1,

如果取消注释代码中的以下两行,则可以删除顶部和右侧轴

axis x line* = bottom,
axis y line* = left,

或者如果你插入

axis lines*=left,

nodes near coords仅获取黄色条,请使用ymax=17000

\addplot+[fill=yellow, draw = yellow,
    nodes near coords,point meta=explicit% <- added
  ] table[x=year, y=Total,
    meta expr=\thisrow{Total}/1000% <- added
  ] {data.txt};

enter image description here

代码:

\begin{filecontents*}{data.txt}
year Donation Equity Lending Reward Mixed-others Total

2010 460.4 49.9 316.5 15.7 0 0
2011 675.7 88.9 554.9 61.5 0 0
2012 979.4 115.7 1169.7 383.3 2.4 0
2013 0 0 0 0 0 6100
2014 0 0 0 0 0 16200
\end{filecontents*}

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\colorlet{dbscan}{purple}
\begin{document}
    \begin{tikzpicture} 
        \begin{axis}[
            legend style={
              legend columns=-1,% <- changed
              at={(xticklabel cs:0.5)},
              anchor=north,
              draw=none
            },
            axis lines*=left,% <- added
            ybar stacked,
            xlabel= Year,
            ylabel = USD bn,
            %ymajorgrids = true,
            width =.90\textwidth,
            height = 1.3\textwidth,
            %xmin = 2005,
            %xmax = 2012,
            ymin = -6,
            ymax = 17000,% <- changed
            try min ticks=17,% <- added
            %ytick={0,1000,...,16000},% <- commented
            xticklabels = none,
            extra x ticks = {2010, 2011, 2012, 2013, 2014},
            extra x tick labels = {2010, 2011, 2012, 2013, 2014},
            legend style={font=\footnotesize},
            bar width=9mm% <- added
            ]
            \addplot+[fill=dbscan, draw = dbscan] table[x=year, y=Donation] {data.txt};
            \addplot+[fill=red, draw = red] table[x=year, y=Equity] {data.txt};
            \addplot+[fill=orange, draw = orange] table[x=year, y=Lending] {data.txt};
            \addplot+[fill=black, draw = black] table[x=year, y=Reward] {data.txt};
            \addplot+[fill=blue, draw = blue] table[x=year, y=Mixed-others] {data.txt};
            \addplot+[fill=yellow, draw = yellow,
                nodes near coords,point meta=explicit% <- added
              ] table[x=year, y=Total,
                meta expr=\thisrow{Total}/1000% <- added
              ] {data.txt};
            \legend{Donation, Equity, Lending, Reward, Mixed other, Total}
            %\coordinate (2013) at (8mm, 100);
        \end{axis} 
        %\node at (2013) {XY};
    \end{tikzpicture}
\end{document}

更新(因为对这个答案的评论)

nodes near coords改变使用的颜色

nodes near coords style={text=black}

并将y值除以 1000,使用

y 表达式=\thisrow{}/1000

对于每一个情节。当然你ytick也必须改变。

enter image description here

代码:

\begin{filecontents*}{data.txt}
year Donation Equity Lending Reward Mixed-others Total

2010 460.4 49.9 316.5 15.7 0 0
2011 675.7 88.9 554.9 61.5 0 0
2012 979.4 115.7 1169.7 383.3 2.4 0
2013 0 0 0 0 0 6100
2014 0 0 0 0 0 16200
\end{filecontents*}

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\colorlet{dbscan}{purple}
\begin{document}
    \begin{tikzpicture} 
        \begin{axis}[
            legend style={
              legend columns=-1,% <- changed
              at={(xticklabel cs:0.5)},
              anchor=north,
              draw=none
            },
            axis lines*=left,% <- added
            ybar stacked,
            xlabel= Year,
            ylabel = USD bn,
            %ymajorgrids = true,
            width =.90\textwidth,
            height = 1.3\textwidth,
            %xmin = 2005,
            %xmax = 2012,
            ymin = 0,
            ymax = 17,% <- changed
            ytick={0,...,17},%< - changed
            xticklabels = none,
            extra x ticks = {2010, 2011, 2012, 2013, 2014},
            extra x tick labels = {2010, 2011, 2012, 2013, 2014},
            legend style={font=\footnotesize},
            bar width=9mm% <- added
            ]
            \addplot+[fill=dbscan, draw = dbscan] table[x=year, y expr=\thisrow{Donation}/1000] {data.txt};
            \addplot+[fill=red, draw = red] table[x=year, y expr=\thisrow{Equity}/1000] {data.txt};
            \addplot+[fill=orange, draw = orange] table[x=year, y expr=\thisrow{Lending}/1000] {data.txt};
            \addplot+[fill=black, draw = black] table[x=year, y expr=\thisrow{Reward}/1000] {data.txt};
            \addplot+[fill=blue, draw = blue] table[x=year, y expr=\thisrow{Mixed-others}/1000] {data.txt};
            \addplot+[fill=yellow, draw = yellow,
                nodes near coords,point meta=explicit,% <- added
                nodes near coords style={text=black}% <- added
              ] table[x=year,
                y expr=\thisrow{Total}/1000,%<- changed
                meta expr=\thisrow{Total}/1000% <- added
              ] {data.txt};
            \legend{Donation, Equity, Lending, Reward, Mixed other, Total}
            %\coordinate (2013) at (8mm, 100);
        \end{axis} 
        %\node at (2013) {XY};
    \end{tikzpicture}
\end{document}

相关内容