我使用以下代码从答案这个帖子产生重复的情节。
是否可以将表格数据包含在当前 tex 文档中,并在所需位置引用它们;而不是使用外部 csv 数据文件?
\documentclass[12pt,a4paper]{article}
\usepackage{geometry}
\geometry{a4paper, left=10mm, right=10mm, top=8mm, bottom=15mm}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\begin{document}
{
1
\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 2, xticklabels at=edge bottom}, height=5cm, width=5cm, ybar=1pt, xtick=data, tick label style={font=\scriptsize}, x tick label style={rotate=45,anchor=east},
symbolic x coords={metric1,metric2,metric3,metric4,metric5,metric6}, ylabel style={align=center}]
\nextgroupplot[title=a,ylabel={set1}, bar width=5pt]
\addplot[fill=blue!40] table[x=metric,y=a1,col sep=space] {colourData1.csv};
\addplot[fill=blue!80] table[x=metric,y=a2,col sep=space] {colourData1.csv};
\nextgroupplot[title=b,bar width=5pt]
\addplot[fill=blue!40] table[x=metric,y=b1,col sep=space] {colourData1.csv};
\addplot[fill=blue!80] table[x=metric,y=b2,col sep=space] {colourData1.csv};
\nextgroupplot[title=c,bar width=5pt]
\addplot[fill=blue!40] table[x=metric,y=c1,col sep=space] {colourData1.csv};
\addplot[fill=blue!80] table[x=metric,y=c2,col sep=space] {colourData1.csv};
\coordinate (mtop) at (rel axis cs:0,1);% coordinate at top of the first plot
\nextgroupplot[ylabel={set2}, bar width=5pt]
\addplot[fill=blue!40] table[x=metric,y=a1,col sep=space] {colourData2.csv};
\addplot[fill=blue!80] table[x=metric,y=a2,col sep=space] {colourData2.csv};
\nextgroupplot[bar width=5pt]
\addplot[fill=blue!40] table[x=metric,y=b1,col sep=space] {colourData2.csv};
\addplot[fill=blue!80] table[x=metric,y=b2,col sep=space] {colourData2.csv};
\nextgroupplot[bar width=5pt]
\addplot[fill=blue!40] table[x=metric,y=c1,col sep=space] {colourData2.csv};
\addplot[fill=blue!80] table[x=metric,y=c2,col sep=space] {colourData2.csv};
\coordinate (mbot) at (rel axis cs:1,0);% coordinate at bottom of the last plot
\end{groupplot}
\path (mtop-|current bounding box.west)-- node[anchor=south,rotate=90,yshift=-0.9cm] {\small Common} (mbot-|current bounding box.west);
\end{tikzpicture}
}
{
2
\pgfplotsset{select row/.style={x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}},}
\pgfplotscreateplotcyclelist{yay-rainbow}{
{black!60!red,fill=white!00!red},
{black!60!red,fill=white!60!red},
{black!60!orange,fill=white!00!orange},
{black!60!orange,fill=white!60!orange},
{black!60!yellow,fill=white!00!yellow},
{black!60!yellow,fill=white!60!yellow},
{black!60!green ,fill=white!00!green },
{black!60!green ,fill=white!60!green },
{black!60!cyan ,fill=white!00!cyan },
{black!60!cyan ,fill=white!60!cyan },
{black!60!violet,fill=white!00!violet},
{black!60!violet,fill=white!60!violet}}
\def\twinplot#1#2{
\pgfplotsforeachungrouped \row in {0,...,5}{
\pgfplotsforeachungrouped \col in {1,...,2}{
\edef\justplotit{
\noexpand\addplot+[bar shift=(\col-1.5)*\pgfplotbarwidth]
table [x=metric, select row=\row, y=#1\col] {#2};}
\justplotit}}}
\begin{tikzpicture}
\begin{groupplot}
[
group style={group size= 3 by 2,xticklabels at=edge bottom},
height=5cm,
width=5cm,
ybar=1pt,
tick label style={font=\scriptsize},
x tick label style={rotate=45,anchor=east},
symbolic x coords={metric1,metric2,metric3,metric4,metric5,metric6},
ylabel style={align=center},
cycle list name=yay-rainbow,
max space between ticks=15,
]
\pgfkeys{/pgf/bar width=5pt}
\nextgroupplot [title=a, ylabel=set1] \twinplot{a}{colourData1.csv}
\nextgroupplot [title=b,] \twinplot{b}{colourData1.csv}
\nextgroupplot [title=c,] \twinplot{c}{colourData1.csv}
\coordinate (mtop) at (rel axis cs:0,1);
\nextgroupplot [ ylabel=set1] \twinplot{a}{colourData2.csv}
\nextgroupplot [] \twinplot{b}{colourData2.csv}
\nextgroupplot [] \twinplot{c}{colourData2.csv}
\coordinate (mbot) at (rel axis cs:1,0);
\end{groupplot}
\path (mtop-|current bounding box.west) --
node [anchor=south, rotate=90, yshift=-0.9cm] {\small Common}
(mbot-|current bounding box.west);
\end{tikzpicture}
}
\end{document}
%the data in the csv files are as follows
\begin{verbatim}
metric a1 a2 b1 b2 c1 c2
metric1 30 60 50 100 50 100
metric2 40 80 60 120 25 50
metric3 50 100 80 160 60 120
metric4 60 120 60 120 89 178
metric5 70 140 25 50 20 40
metric6 80 160 30 60 23 46
\end{verbatim}
答案1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotscreateplotcyclelist{yay-rainbow}{
{black!60!red, fill=white!00!red},
{black!60!red, fill=white!60!red},
{black!60!orange, fill=white!00!orange},
{black!60!orange, fill=white!60!orange},
{black!60!yellow, fill=white!00!yellow},
{black!60!yellow, fill=white!60!yellow},
{black!60!green ,fill=white!00!green},
{black!60!green ,fill=white!60!green},
{black!60!cyan ,fill=white!00!cyan},
{black!60!cyan ,fill=white!60!cyan},
{black!60!violet, fill=white!00!violet},
{black!60!violet, fill=white!60!violet},
}
\pgfplotsset{select row/.style={x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}},}
\def\twinplot#1#2{
\pgfplotsforeachungrouped \row in {0,...,5}{
\addplot+[bar shift=(1-1.5)*\pgfplotbarwidth] table [x=metric, select row=\row, y=#11] {#2};
\addplot+[bar shift=(2-1.5)*\pgfplotbarwidth] table [x=metric, select row=\row, y=#12] {#2};
}}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
metric a1 a2 b1 b2 c1 c2
metric1 30 60 50 100 50 100
metric2 40 80 60 120 25 50
metric3 50 100 80 160 60 120
metric4 60 120 60 120 89 178
metric5 70 140 25 50 20 40
metric6 80 160 30 60 23 46
}\colourData
\begin{groupplot}[
group style={group size= 3 by 2,
xticklabels at=edge bottom},
height=5cm,
width=5cm,
ybar=1pt,
tick label style={font=\scriptsize},
x tick label style={rotate=45, anchor=east},
symbolic x coords={metric1, metric2, metric3, metric4, metric5, metric6},
ylabel style={align=center},
cycle list name=yay-rainbow,
max space between ticks=15,
/pgf/bar width=5pt,
]
\nextgroupplot[title=a, ylabel=set1] \twinplot{a}{\colourData}
\nextgroupplot[title=b,] \twinplot{b}{\colourData}
\nextgroupplot[title=c,] \twinplot{c}{\colourData}
\nextgroupplot[ylabel=set1] \twinplot{a}{\colourData}
\nextgroupplot[] \twinplot{b}{\colourData}
\nextgroupplot[] \twinplot{c}{\colourData}
\end{groupplot}
\end{tikzpicture}
\end{document}