You simply need to adjust the height of the color bar
plot.
This can easily be accomplished by doing:
every colorbar/.append style={height=<height specification>}
However, often you will use different heights for different groupplots.
Therefore a more general approach would be:
every colorbar/.append style={height=
2*\pgfkeysvalueof{/pgfplots/parent axis height}+
\pgfkeysvalueof{/pgfplots/group/vertical sep}
}
The first key /pgfplots/parent axis height
is the height of the actual contour plot (of which you attach the colorbar).
The second key /pgfplots/group/vertical sep
is the spacing in between the two group plots. This needs to be adjusted by the number of lower lying plots, in this case only one.
So the above takes and makes the colorbar plot have a height equal to 2 plots plus the vertical separation of the plots. Furthermore it is generic if you change height and vertical sep, etc.
However, notice that if the plots have different heights the above approach will not work.
Ok, so one thing is that you only need to plot the colorbar once. Groupplots ensure that each statement in the groupplot
environment will be added to every sub-plot. Hence you simply need to put the colorbar in the top \nextgroupplot
. The colorbar is aligned at the top right corner of the parent axis, hence you should do it to the top plot.
So:
\nextgroupplot[colorbar right,
every colorbar/.append style={height=
2*\pgfkeysvalueof{/pgfplots/parent axis height}+
\pgfkeysvalueof{/pgfplots/group/vertical sep}}]
A thing to be very careful about when attaching colorbars to several contours is to ensure the bounds of the contour to be the same for both, hence I would recommend you to also add point meta min=<value>
and point meta max=<value>
. Please do remember this! :)
Ok, so the final groupplots
is something like this:
\begin{tikzpicture}
\begin{groupplot}[view={0}{90},
xlabel=$x$,
ylabel=$y$,
height=3cm,
point meta min=-1.5,
point meta max=1.5,
group/xlabels at = edge bottom,
group style = {group size = 1 by 2,
xlabels at = edge bottom
}]
\nextgroupplot[colorbar right,
every colorbar/.append style={height=
2*\pgfkeysvalueof{/pgfplots/parent axis height}+
\pgfkeysvalueof{/pgfplots/group/vertical sep}}]
\addplot3[surf] {sin(36*x)};
\nextgroupplot
\addplot3[surf] {cos(36*y)};
\end{groupplot}
\end{tikzpicture}
This will result in:
I took a parula scale written by Jake and defined the colormap
to use it. The curves are plotted with a for-each loop. I set the meta data of the points to the slope variable. The option mesh
plots the lines with the colour of its meta data.
% arara: pdflatex
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{%
,compat=1.11
,colormap={parula}{%
rgb=(0.2081,0.1663,0.5292)rgb=(0.2116,0.1898,0.5777)rgb=(0.2123,0.2138,0.627)
rgb=(0.2081,0.2386,0.6771)rgb=(0.1959,0.2645,0.7279)rgb=(0.1707,0.2919,0.7792)
rgb=(0.1253,0.3242,0.8303)rgb=(0.0591,0.3598,0.8683)rgb=(0.0117,0.3875,0.882)
rgb=(0.006,0.4086,0.8828) rgb=(0.0165,0.4266,0.8786)rgb=(0.0329,0.443,0.872)
rgb=(0.0498,0.4586,0.8641)rgb=(0.0629,0.4737,0.8554)rgb=(0.0723,0.4887,0.8467)
rgb=(0.0779,0.504,0.8384) rgb=(0.0793,0.52,0.8312) rgb=(0.0749,0.5375,0.8263)
rgb=(0.0641,0.557,0.824) rgb=(0.0488,0.5772,0.8228)rgb=(0.0343,0.5966,0.8199)
rgb=(0.0265,0.6137,0.8135)rgb=(0.0239,0.6287,0.8038)rgb=(0.0231,0.6418,0.7913)
rgb=(0.0228,0.6535,0.7768)rgb=(0.0267,0.6642,0.7607)rgb=(0.0384,0.6743,0.7436)
rgb=(0.059,0.6838,0.7254) rgb=(0.0843,0.6928,0.7062)rgb=(0.1133,0.7015,0.6859)
rgb=(0.1453,0.7098,0.6646)rgb=(0.1801,0.7177,0.6424)rgb=(0.2178,0.725,0.6193)
rgb=(0.2586,0.7317,0.5954)rgb=(0.3022,0.7376,0.5712)rgb=(0.3482,0.7424,0.5473)
rgb=(0.3953,0.7459,0.5244)rgb=(0.442,0.7481,0.5033) rgb=(0.4871,0.7491,0.484)
rgb=(0.53,0.7491,0.4661) rgb=(0.5709,0.7485,0.4494)rgb=(0.6099,0.7473,0.4337)
rgb=(0.6473,0.7456,0.4188)rgb=(0.6834,0.7435,0.4044)rgb=(0.7184,0.7411,0.3905)
rgb=(0.7525,0.7384,0.3768)rgb=(0.7858,0.7356,0.3633)rgb=(0.8185,0.7327,0.3498)
rgb=(0.8507,0.7299,0.336) rgb=(0.8824,0.7274,0.3217)rgb=(0.9139,0.7258,0.3063)
rgb=(0.945,0.7261,0.2886) rgb=(0.9739,0.7314,0.2666)rgb=(0.9938,0.7455,0.2403)
rgb=(0.999,0.7653,0.2164) rgb=(0.9955,0.7861,0.1967)rgb=(0.988,0.8066,0.1794)
rgb=(0.9789,0.8271,0.1633)rgb=(0.9697,0.8481,0.1475)rgb=(0.9626,0.8705,0.1309)
rgb=(0.9589,0.8949,0.1132)rgb=(0.9598,0.9218,0.0948)rgb=(0.9661,0.9514,0.0755)
rgb=(0.9763,0.9831,0.0538)
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
,ymin=0,xmin=0,ymax=5,xmax=1
,colorbar
,colorbar style={ylabel=line slope}
]
\foreach \m in {1,1.2,...,5} % you will need {1,...,5} here, but it looked so beautiful...
{\edef\temp{\noexpand\addplot[mesh,point meta=\m] {x*\m};}\temp}
\end{axis}
\end{tikzpicture}
\end{document}
As asked in your comment, here is a solution loading data from files:
% arara: pdflatex
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{filecontents}
\begin{filecontents}{data_1.dat}
0 0
1 1
\end{filecontents}
\begin{filecontents}{data_2.dat}
0 0
1 2
\end{filecontents}
\begin{filecontents}{data_3.dat}
0 0
1 3
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
,ymin=0,xmin=0,ymax=5,xmax=1
,colorbar
,colorbar style={ylabel=line slope}
]
\foreach \m in {1,...,3}
{\edef\temp{\noexpand\addplot[mesh,point meta=\m] table {data_\m.dat};}\temp}
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
I came to a solution. I made an
axis
environment large as 3 axis and high as 2 axis and I put its colorbar on top usingat
andanchor
: