首先,(正如您马上就会清楚看到的)我是一个 LaTeX 新手——走到这一步花费了我相当多的时间,但现在我陷入了困境。
梅威瑟:https://www.overleaf.com/read/gqwjpqnggvcc
我正在努力适应这个很好的例子使用水平条而不是垂直条。如您所见,我的条形图开始移动(至少对我来说,这是非常令人惊讶的方式),并且我的 x 轴缩放似乎偏离了方向。我的目标是所有图表都具有相同的条目顺序(绿色始终在顶部,青绿色始终在底部等),并且只在所包含的数据方面有所不同。同样,每个 x 轴都应自动缩放,就像在 3 个图中的第一个中一样。
有人能给我指点一下我应该从哪里开始吗?
谢谢!
\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotstableread{
Criterion Rapperswil Palermo Jeddah
Turquoise 3620 15749 42754
Yellow 2739 15109 42240
Purple 3710 16146 40533
Red 714 9306 28919
Blue 2997 6840 11614
Orange 1589 4992 11786
Green 2122 11154 28747
}\datatable
% Retrieve the number of rows from the datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathsetmacro{\rows}{\pgfplotsretval-1}
\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}
\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2},
{RYB3!50!black,fill=RYB3},
{RYB4!50!black,fill=RYB4},
{RYB5!50!black,fill=RYB5},
{RYB6!50!black,fill=RYB6},
{RYB7!50!black,fill=RYB7},
}
\pgfplotsset{
select row/.style={
x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
}
}
\begin{document}
All corresponding bars should be on the same position in each respective chart - green on top, turquoise on the bottom.
\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{groupplot}
[
group style={
group size=2 by 2,
% columns=2,
% rows=2,
xlabels at=edge bottom,
ylabels at=edge left,
horizontal sep=0.05\textwidth, group name=plots
},
/pgf/number format/1000 sep={},
xbar,
xmin=0,
ytick={0,...,\rows},
yticklabels from table={\datatable}{Criterion},
xtick pos=bottom,
% y=0.1pt,
%
% ybar=0pt,
/pgf/bar shift=0pt,scale only axis,
width=\textwidth/3,
% yticklabel style={text width=0.035\textwidth, align=right, inner xsep=0pt, xshift=-0.005\textwidth},
% ylabel=kWh, ylabel style={text height=0.02\textwidth,inner ysep=0pt},
% enlarge x limits=0.15,
% ymin=0,
% xtick=\empty,
% tickpos=left,
% scaled y ticks=base 10:-3,
cycle list name=colorbrewer-RYB,
legend columns=-1,
legend style={draw=none, /tikz/every even column/.append style={column sep=5pt}},
legend image code/.code={%
\draw[#1] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
}
]
\nextgroupplot[xlabel=Rapperswil, legend to name=grouplegend,]
\pgfplotsinvokeforeach {0,...,\rows}{
\addplot table [ y expr=\coordindex, select row=#1, x=Rapperswil] {\datatable};
\addlegendentry {\pgfplotstablegetelem{#1}{Criterion}\of\datatable \pgfplotsretval}
}
\nextgroupplot[xlabel=Palermo]
\pgfplotsinvokeforeach {0,...,\rows}{
\addplot table [x expr=\coordindex, select row=#1, y=Palermo] {\datatable};
}
\nextgroupplot[xlabel=Jeddah]
\pgfplotsinvokeforeach {0,...,\rows}{
\addplot table [x expr=\coordindex, select row=#1, y=Jeddah] {\datatable};
}
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
我不太确定这是你想要的(我不得不修改一些table
选项,所以请检查),但是这是添加后得到的结果/pgf/bar shift=0pt,/pgf/bar width=0.9
。
\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotstableread{
Criterion Rapperswil Palermo Jeddah
Turquoise 3620 15749 42754
Yellow 2739 15109 42240
Purple 3710 16146 40533
Red 714 9306 28919
Blue 2997 6840 11614
Orange 1589 4992 11786
Green 2122 11154 28747
}\datatable
% Retrieve the number of rows from the datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathsetmacro{\rows}{\pgfplotsretval-1}
\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}
\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2},
{RYB3!50!black,fill=RYB3},
{RYB4!50!black,fill=RYB4},
{RYB5!50!black,fill=RYB5},
{RYB6!50!black,fill=RYB6},
{RYB7!50!black,fill=RYB7},
}
\pgfplotsset{
select row/.style={
x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
}
}
\begin{document}
All corresponding bars should be on the same position in each respective chart - green on top, turquoise on the bottom.
\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{groupplot}
[
group style={
group size=2 by 2,
% columns=2,
% rows=2,
xlabels at=edge bottom,
ylabels at=edge left,
horizontal sep=0.2\textwidth, group name=plots
},
/pgf/number format/1000 sep={},
xbar,
/pgf/bar shift=0pt,
/pgf/bar width=0.9,
xmin=0,
ytick={0,...,\rows},
yticklabels from table={\datatable}{Criterion},
xtick pos=bottom,
% y=0.1pt,
%
% ybar=0pt,
%bar shift=0pt,
scale only axis,
width=\textwidth/3,
% yticklabel style={text width=0.035\textwidth, align=right, inner xsep=0pt, xshift=-0.005\textwidth},
% ylabel=kWh, ylabel style={text height=0.02\textwidth,inner ysep=0pt},
% enlarge x limits=0.15,
% ymin=0,
% xtick=\empty,
% tickpos=left,
% scaled y ticks=base 10:-3,
cycle list name=colorbrewer-RYB,
legend columns=-1,
legend style={draw=none, /tikz/every even column/.append style={column sep=5pt}},
legend image code/.code={%
\draw[#1] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
}
]
\nextgroupplot[xlabel=Rapperswil, legend to name=grouplegend,]
\pgfplotsinvokeforeach {0,...,\rows}{
\addplot table [ y expr=\coordindex, select row=#1, x=Rapperswil] {\datatable};
\addlegendentry {\pgfplotstablegetelem{#1}{Criterion}\of\datatable \pgfplotsretval}
}
\nextgroupplot[xlabel=Palermo]
\pgfplotsinvokeforeach {0,...,\rows}{
\addplot table [y expr=\coordindex, select row=#1, x=Palermo] {\datatable};
}
\nextgroupplot[xlabel=Jeddah]
\pgfplotsinvokeforeach {0,...,\rows}{
\addplot table [y expr=\coordindex, select row=#1, x=Jeddah] {\datatable};
}
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}