抱歉,这个例子太长了,但是当我尝试缩短它时,我把事情搞乱了。不要介意重叠的条。如果在主文档中使用,线条的大小是合适的。
我一直试图在图例上方放置一些文本,这种方法可行(第 77 行和第 90 行)。但是,我希望节点自动位于图例上方的中心。我发现这但未能成功将其转移到我的问题中。名称“legend”(设置在第 66 行)据说是未知的。
期望结果:
\documentclass[dvipsnames, border=5pt]{standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
RoomA Oc Cc Bc A Bh Ch Oh
Porto 0.000 0.001 0.017 0.907 0.061 0.015 0.000
Madrid 0.000 0.008 0.010 0.857 0.125 0.000 0.000
Larnaca 0.000 0.005 0.013 0.749 0.233 0.000 0.000
Copenhagen 0.000 0.000 0.004 0.966 0.030 0.000 0.000
Belgrade 0.000 0.002 0.018 0.886 0.094 0.000 0.000
Banja~Luka 0.000 0.000 0.004 0.953 0.043 0.000 0.000
}\roomA
\pgfplotstableread{ % Read the data into a table macro
RoomB Oc Cc Bc A Bh Ch Oh
Porto 0.01 0.07 0.01 0.90 0.02 0.00 0.00
Madrid 0.00 0.00 0.01 0.97 0.02 0.00 0.00
Larnaca 0.00 0.01 0.02 0.98 0.00 0.00 0.00
Copenhagen 0.00 0.02 0.03 0.95 0.00 0.00 0.00
Belgrade 0.00 0.01 0.03 0.96 0.00 0.00 0.00
Banja~Luka 0.00 0.00 0.01 0.78 0.21 0.00 0.00
}\roomB
\pgfplotstableread{ % Read the data into a table macro
RoomC Oc Cc Bc A Bh Ch Oh
Porto 0.00 0.00 0.01 0.86 0.11 0.03 0.00
Madrid 0.00 0.00 0.04 0.87 0.08 0.01 0.00
Larnaca 0.00 0.00 0.01 0.76 0.20 0.04 0.00
Copenhagen 0.00 0.00 0.02 0.94 0.04 0.00 0.00
Belgrade 0.00 0.00 0.04 0.90 0.06 0.00 0.00
Banja~Luka 0.00 0.00 0.00 0.83 0.17 0.00 0.00
}\roomC
\pgfplotsset{xbar stacked, % Stacked horizontal bars
width=0.9\textwidth,
xmin=0, % Start x axis at 0
xmax=100,
ymin=-0.75, % Start x axis at 0
ymax=5.75,
%xtick=\empty,
%ytick=\empty,
ytick=data, % Use as many tick labels as y coordinates
%yticklabels from table={\roomA}{RoomA}, % Get the labels from the Label column of the \datatable
unit vector ratio=1 11, % changing the aspect ratio of the immage
}
\begin{axis}[bar shift=11, bar width=8, axis x line = none, axis y line = none]
\addplot [fill=red] table [x expr=100*\thisrow{Oc}, y expr=\coordindex] {\roomA};
\addplot [fill=purple] table [x expr=100*\thisrow{Cc}, y expr=\coordindex] {\roomA};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bc}, y expr=\coordindex] {\roomA};
\addplot [fill=SeaGreen] table [x expr=100*\thisrow{A}, y expr=\coordindex] {\roomA};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bh}, y expr=\coordindex] {\roomA};
\addplot [fill=purple] table [x expr=100*\thisrow{Ch}, y expr=\coordindex] {\roomA};
\addplot [fill=red] table [x expr=100*\thisrow{Oh}, y expr=\coordindex] {\roomA};
\end{axis}
\begin{axis}[bar shift=-11, bar width=8, axis x line = none, axis y line = none, legend style={draw=none,
fill=none,
at={(0.5,1.02)},
anchor= south,
name=legend,
},
legend columns={7}, area legend]
\addplot [fill=red] table [x expr=100*\thisrow{Oc}, y expr=\coordindex] {\roomC};
\addplot [fill=purple] table [x expr=100*\thisrow{Cc}, y expr=\coordindex] {\roomC};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bc}, y expr=\coordindex] {\roomC};
\addplot [fill=SeaGreen] table [x expr=100*\thisrow{A}, y expr=\coordindex] {\roomC};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bh}, y expr=\coordindex] {\roomC};
\addplot [fill=purple] table [x expr=100*\thisrow{Ch}, y expr=\coordindex] {\roomC};
\addplot [fill=red] table [x expr=100*\thisrow{Oh}, y expr=\coordindex] {\roomC};
\legend{a, b, c, d, e, f, g} %Bh, Ch,Oh
\coordinate(aboveLegend) at (axis cs:50,6.6);
\end{axis}
\begin{axis}[bar shift=0, bar width=8, ytick=data, yticklabels from table={\roomA}{RoomA}, xtick={0,20,...,100}, xlabel={time [\%]}]
\addplot [fill=red] table [x expr=100*\thisrow{Oc}, y expr=\coordindex] {\roomB};
\addplot [fill=purple] table [x expr=100*\thisrow{Cc}, y expr=\coordindex] {\roomB};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bc}, y expr=\coordindex] {\roomB};
\addplot [fill=SeaGreen] table [x expr=100*\thisrow{A}, y expr=\coordindex] {\roomB};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bh}, y expr=\coordindex] {\roomB};
\addplot [fill=purple] table [x expr=100*\thisrow{Ch}, y expr=\coordindex] {\roomB};
\addplot [fill=red] table [x expr=100*\thisrow{Oh}, y expr=\coordindex] {\roomB};
\end{axis}
\node at (aboveLegend){Climate categories };
\end{tikzpicture}
\end{document}
答案1
注意,这 legend
只是一个tikz
node
,您可以给它贴上标签:
legend style={draw=none,
fill=none,
at={(0.5,1.02)},
anchor= south,
name=legend,
label=above : Climate categories %%% <----- here
},
完整代码:
\documentclass[dvipsnames, border=5pt]{standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
RoomA Oc Cc Bc A Bh Ch Oh
Porto 0.000 0.001 0.017 0.907 0.061 0.015 0.000
Madrid 0.000 0.008 0.010 0.857 0.125 0.000 0.000
Larnaca 0.000 0.005 0.013 0.749 0.233 0.000 0.000
Copenhagen 0.000 0.000 0.004 0.966 0.030 0.000 0.000
Belgrade 0.000 0.002 0.018 0.886 0.094 0.000 0.000
Banja~Luka 0.000 0.000 0.004 0.953 0.043 0.000 0.000
}\roomA
\pgfplotstableread{ % Read the data into a table macro
RoomB Oc Cc Bc A Bh Ch Oh
Porto 0.01 0.07 0.01 0.90 0.02 0.00 0.00
Madrid 0.00 0.00 0.01 0.97 0.02 0.00 0.00
Larnaca 0.00 0.01 0.02 0.98 0.00 0.00 0.00
Copenhagen 0.00 0.02 0.03 0.95 0.00 0.00 0.00
Belgrade 0.00 0.01 0.03 0.96 0.00 0.00 0.00
Banja~Luka 0.00 0.00 0.01 0.78 0.21 0.00 0.00
}\roomB
\pgfplotstableread{ % Read the data into a table macro
RoomC Oc Cc Bc A Bh Ch Oh
Porto 0.00 0.00 0.01 0.86 0.11 0.03 0.00
Madrid 0.00 0.00 0.04 0.87 0.08 0.01 0.00
Larnaca 0.00 0.00 0.01 0.76 0.20 0.04 0.00
Copenhagen 0.00 0.00 0.02 0.94 0.04 0.00 0.00
Belgrade 0.00 0.00 0.04 0.90 0.06 0.00 0.00
Banja~Luka 0.00 0.00 0.00 0.83 0.17 0.00 0.00
}\roomC
\pgfplotsset{xbar stacked, % Stacked horizontal bars
width=0.9\textwidth,
xmin=0, % Start x axis at 0
xmax=100,
ymin=-0.75, % Start x axis at 0
ymax=5.75,
%xtick=\empty,
%ytick=\empty,
ytick=data, % Use as many tick labels as y coordinates
%yticklabels from table={\roomA}{RoomA}, % Get the labels from the Label column of the \datatable
unit vector ratio=1 11, % changing the aspect ratio of the immage
}
\begin{axis}[bar shift=11, bar width=8, axis x line = none, axis y line = none]
\addplot [fill=red] table [x expr=100*\thisrow{Oc}, y expr=\coordindex] {\roomA};
\addplot [fill=purple] table [x expr=100*\thisrow{Cc}, y expr=\coordindex] {\roomA};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bc}, y expr=\coordindex] {\roomA};
\addplot [fill=SeaGreen] table [x expr=100*\thisrow{A}, y expr=\coordindex] {\roomA};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bh}, y expr=\coordindex] {\roomA};
\addplot [fill=purple] table [x expr=100*\thisrow{Ch}, y expr=\coordindex] {\roomA};
\addplot [fill=red] table [x expr=100*\thisrow{Oh}, y expr=\coordindex] {\roomA};
\end{axis}
\begin{axis}[bar shift=-11, bar width=8, axis x line = none, axis y line = none, legend style={draw=none,
fill=none,
at={(0.5,1.02)},
anchor= south,
name=legend,
label=above : Climate categories %%% <----- here
},
legend columns={7}, area legend]
\addplot [fill=red] table [x expr=100*\thisrow{Oc}, y expr=\coordindex] {\roomC};
\addplot [fill=purple] table [x expr=100*\thisrow{Cc}, y expr=\coordindex] {\roomC};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bc}, y expr=\coordindex] {\roomC};
\addplot [fill=SeaGreen] table [x expr=100*\thisrow{A}, y expr=\coordindex] {\roomC};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bh}, y expr=\coordindex] {\roomC};
\addplot [fill=purple] table [x expr=100*\thisrow{Ch}, y expr=\coordindex] {\roomC};
\addplot [fill=red] table [x expr=100*\thisrow{Oh}, y expr=\coordindex] {\roomC};
\legend{a, b, c, d, e, f, g} %Bh, Ch,Oh
%\coordinate(aboveLegend) at (axis cs:50,6.6);
\end{axis}
\begin{axis}[bar shift=0, bar width=8, ytick=data, yticklabels from table={\roomA}{RoomA}, xtick={0,20,...,100}, xlabel={time [\%]}]
\addplot [fill=red] table [x expr=100*\thisrow{Oc}, y expr=\coordindex] {\roomB};
\addplot [fill=purple] table [x expr=100*\thisrow{Cc}, y expr=\coordindex] {\roomB};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bc}, y expr=\coordindex] {\roomB};
\addplot [fill=SeaGreen] table [x expr=100*\thisrow{A}, y expr=\coordindex] {\roomB};
\addplot [fill=yellow] table [x expr=100*\thisrow{Bh}, y expr=\coordindex] {\roomB};
\addplot [fill=purple] table [x expr=100*\thisrow{Ch}, y expr=\coordindex] {\roomB};
\addplot [fill=red] table [x expr=100*\thisrow{Oh}, y expr=\coordindex] {\roomB};
\end{axis}
%\node at (aboveLegend){Climate categories};
\end{tikzpicture}
\end{document}