我想在条形图中循环显示每个 x 坐标的颜色。可以吗?例如,在此示例中,我希望每个指标都以不同的颜色显示。
这个问题groupplot/barplot 中的颜色和图例做的事情非常相似,只是在我的每个循环中,我想添加两个条(一个浅色和一个深色)。
\documentclass{article}
\usepackage[hscale=0.9]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,shapes.geometric,arrows,fit,matrix,positioning,pgfplots.groupplots,trees,patterns}
\begin{document}
\begin{figure*}
\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}
\caption{group plot with each plot in a different colour}
\label{fig:metrics}
\end{figure*}
\end{document}
更新
对于此组图中的单个图,有六个指标。每个指标都有两个条形图。例如,在左上角的图中,对于所有 6 个指标,绘制了 a1 和 a2。同样,对于右下角的图,绘制了 c1 和 c2。
我希望每个指标都以不同的颜色绘制。但一个指标的两个条形图应以相同颜色的深色和浅色绘制。
注意:如果这样更容易绘图,我可以转置输入数据。
颜色数据1.csv:
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
colourData2.csv 是 colourData1.csv 的克隆。
答案1
这只是一个版本你链接到的答案量身定制以适合您。需要一点技巧(max space between ticks
)才能使 x 轴上的刻度正常。
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\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{document}
\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}