如何切換堆積長條圖中的行/列

如何切換堆積長條圖中的行/列

這是我的 MWE:

\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label   First   Second  Third
Yes      0.1     0.3     0.3
No      0.9     0.7     0.7
}\datatable

\begin{axis}[
    xbar stacked, % Stacked horizontal bars
    xmin=0, % Start x axis at 0
    ytick=data, % Use as many tick labels as y coordinates
    yticklabels from table={\datatable}{Label} % Get the labels from the Label column of the \datatable
]
\addplot [fill=yellow] table [x=First, y expr=\coordindex] {\datatable}; % Plot the "First" column against the data index
\addplot [fill=green!70!blue] table [x=Second, y expr=\coordindex] {\datatable};
\addplot [fill=red!80!yellow] table [x=Third, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}

此腳本產生一個圖表,其中 y 軸對應於列的是/否值第一的,第二, 和第三。如何更改腳本以使 y 軸對應於列(即第一的,第二, 和第三)?我需要水平表示特定列的是/否分佈。

答案1

我找到了一個解決方案調換表格並修改腳本如下:

\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label First Second Third
Yes 0.1 0.3 0.3
No 0.9 0.7 0.7
}\datatable

\pgfplotstabletranspose[string type, colnames from=Label, input colnames to=Label]\datatabletransposed{\datatable}

\begin{axis}[
    xbar stacked, % Stacked horizontal bars
    xmin=0, % Start x axis at 0
    ytick=data, % Use as many tick labels as y coordinates
    yticklabels from table={\datatabletransposed}{Label} % Get the labels from the Label column of the \datatable
]
\addplot [fill=green!70!blue] table [x=Yes, y expr=\coordindex] {\datatabletransposed};
\addplot [fill=red!70!blue] table [x=No, y expr=\coordindex] {\datatabletransposed};
\end{axis}
\end{tikzpicture}

相關內容