條形圖:變更不同圖和對應圖例條目的垂直順序

條形圖:變更不同圖和對應圖例條目的垂直順序

我想使用水平條形圖來比較兩個不同年份的百分比。

兩年(1996 年和 2011 年)的值透過 pgfplots 表給出。我希望 1996 年的值顯示在相應的 2011 年值的頂部。但是,當以正確的順序添加它們時,圖例條目將以另一種方式排序(首先顯示 2011 年,然後顯示 1996 年)。

微量元素:

\documentclass[a4paper,11pt,twoside]{memoir}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread[col sep=comma,header=true]{
Type,1996,2011
type1,41.26,46.57
type2,55.42,38.76
type3,0.14,0.11
type4,0.24,0.05
type5,0.79,13.20
type6,2.14,1.31
}\data

\begin{figure} [tb]%
    \centering
    \begin{tikzpicture}     
        \begin{axis}[    
            width=12cm,
            xbar,                                 
            xtick={0,10,20,...,100},    
            xmin=0,
            xmax=100,       
            grid=major,
            nodes near coords, nodes near coords align={horizontal},
            symbolic y coords={type6,type5,type4,type3,type2,type1},
            ylabel={Type},
            xlabel={Percentage},
            y label style={at={(-0.1,0.5)}},
            enlarge x limits={abs=0}
        ]
        \addplot table [x=1996, y=Type] {\data};
        \addplot table [x=2011, y=Type] {\data};
        \legend{1996,2011}
        \end{axis}
    \end{tikzpicture}
\label{fig:distribution}
\end{figure}

\end{document}

我嘗試手動更改圖例順序,如中所述這個帖子,但這導致圖例中的條形和文字之間出現醜陋的垂直對齊:

\documentclass[a4paper,11pt,twoside]{memoir}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread[col sep=comma,header=true]{
Type,1996,2011
type1,41.26,46.57
type2,55.42,38.76
type3,0.14,0.11
type4,0.24,0.05
type5,0.79,13.20
type6,2.14,1.31
}\data

\begin{figure} [tb]%
    \centering
    \begin{tikzpicture}     
        \begin{axis}[    
            width=12cm,
            xbar,                                 
            xtick={0,10,20,...,100},    
            xmin=0,
            xmax=100,       
            grid=major,
            nodes near coords, nodes near coords align={horizontal},
            symbolic y coords={type6,type5,type4,type3,type2,type1},
            ylabel={Type},
            xlabel={Percentage},
            y label style={at={(-0.1,0.5)}},
            enlarge x limits={abs=0},
            extra description/.code={
         \matrix[/pgfplots/every axis legend]
         {
                        \ref{1996} \pgfmatrixnextcell \node{1996};\\
                        \ref{2011} \pgfmatrixnextcell \node{2011};\\             
         };
         } 
        ]       
        \addplot table [x=2011, y=Type] {\data};        
        \label{2011}        
        \addplot table [x=1996, y=Type] {\data};
        \label{1996}
        \end{axis}
    \end{tikzpicture}
\label{fig:distribution}
\end{figure}

\end{document}

正如帖子中所建議的,我查閱了tikz手冊,但沒有找到解決這個問題的方法(至少對於我的乳膠技能)。有什麼提示嗎?

答案1

有一個叫做它的鍵reverse legend應該完全符合您的要求。

將其新增至選項清單會產生

\documentclass[a4paper,11pt,twoside]{memoir}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\thispagestyle{empty}

\pgfplotstableread[col sep=comma,header=true]{
Type,1996,2011
type1,41.26,46.57
type2,55.42,38.76
type3,0.14,0.11
type4,0.24,0.05
type5,0.79,13.20
type6,2.14,1.31
}\data

\begin{figure} [tb]%
    \centering
    \begin{tikzpicture}     
        \begin{axis}[    
            width=12cm,
            xbar,                                 
            xtick={0,10,20,...,100},    
            xmin=0,
            xmax=100,       
            grid=major,
            nodes near coords, nodes near coords align={horizontal},
            symbolic y coords={type6,type5,type4,type3,type2,type1},
            ylabel={Type},
            xlabel={Percentage},
            y label style={at={(-0.1,0.5)}},
            enlarge x limits={abs=0},
            reverse legend,
        ]
        \addplot table [x=2011, y=Type] {\data};
        \addplot table [x=1996, y=Type] {\data};
        \legend{2011,1996}
        \end{axis}
    \end{tikzpicture}
\label{fig:distribution}
\end{figure}

\end{document}

在此輸入影像描述

相關內容