You can use
y filter/.expression={y==0 ? nan : y}
in the options of \addplot
.
\documentclass{article}
% ---------------------------------- tikz
\usepackage{pgfplots} % to print charts
\pgfplotsset{compat=1.12}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis} [
% general
ybar,
scale only axis,
height=0.5\textwidth,
width=1.2\textwidth,
ylabel={\# Dots},
nodes near coords,
xlabel={Variation},
xticklabel style={
rotate=90,
anchor=east,
},
%enlarge x limits={abs value={3}},
]
\addplot+[y filter/.expression={y==0 ? nan : y}] table [
x=grade,
y=value,
] {
grade value
-11 0
-10 0
-9 0
-8 0
-7 0
-6 0
-5 3
-4 1
-3 2
-2 15
-1 11
0 179
1 8
2 1
3 0
4 1
5 2
6 0
7 0
8 0
9 0
10 0
11 0
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
Ok, I basically got what I want with the second attempt. But the solution is not as automated as I would like it to have concerning width and position of the colorbar
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\xmin}{0}
\pgfmathsetmacro{\xmax}{1.64}
\pgfmathsetmacro{\ymin}{0}
\pgfmathsetmacro{\ymax}{1}
\begin{groupplot}[
group style={
group name=my plots,
group size= 2 by 4,
vertical sep=1.5cm
},
width=7cm,
hide axis,
enlargelimits=false,
axis equal image,
]
\nextgroupplot
\addplot graphics [xmin=\xmin,xmax=\xmax,ymin=\ymin,ymax=\ymax]
{example-image-a};
\coordinate (top) at (rel axis cs:0,1);% coordinate at top of the first plot
\nextgroupplot
\addplot graphics [xmin=\xmin,xmax=\xmax,ymin=\ymin,ymax=\ymax]
{example-image-a};
\nextgroupplot
\addplot graphics [xmin=\xmin,xmax=\xmax,ymin=\ymin,ymax=\ymax]
{example-image-a};
\nextgroupplot
\addplot graphics [xmin=\xmin,xmax=\xmax,ymin=\ymin,ymax=\ymax]
{example-image-a};
\coordinate (bot) at (rel axis cs:1,0);% coordinate at bottom of the last plot
\end{groupplot}
% Labels
\node[below = 0.5cm of my plots c1r1.south] {(a) Timestep 25};
\node[below = 0.5cm of my plots c2r1.south] {(b) Timestep 35};
\node[below = 0.5cm of my plots c1r2.south] {(c) Timestep 45};
\node[below = 0.5cm of my plots c2r2.south] {(d) Timestep 55};
% Node position middle right groupplot
\path (top-|current bounding box.east)--
coordinate(legendposright)
(bot-|current bounding box.east);
% Node position middle above groupplot
\path (top|-current bounding box.north)--
coordinate(legendposabove)
(bot|-current bounding box.north);
% Node markers
%\node [circle,fill=black,minimum size=10pt] at (legendposright){};
%\node [circle,fill=black,minimum size=10pt] at (legendposabove){};
%Colorbar above
\begin{axis}[%
hide axis,
scale only axis,
height=.01\linewidth,
width=0.975\linewidth,
at={(legendposabove.south)},
yshift=1.25cm,
anchor=south,
point meta min=0.0,
point meta max=2.0,
colormap/bluered, % Colormap preset
colorbar horizontal, % Active colorbar
colorbar sampled, % Steps in colorbar
colorbar style={
separate axis lines,
samples=256, % Number of steps
xticklabel pos=upper
},
%every colorbar/.append style={
% width=\pgfkeysvalueof{/pgfplots/parent axis width}%+
% %\pgfkeysvalueof{/pgfplots/group/vertical sep}
%}
]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
%Colorbar right
\begin{axis}[%
hide axis,
scale only axis,
%height=0.975\linewidth,
%width=0.01\linewidth,
at={(legendposright.east)},
anchor=east,
xshift=0.25cm,
point meta min=0.0,
point meta max=2.0,
colormap/bluered, % Colormap preset
colorbar right, % Active colorbar
colorbar sampled, % Steps in colorbar
colorbar style={
separate axis lines,
samples=256, % Number of steps
},
every colorbar/.append style={
height=\pgfkeysvalueof{/pgfplots/parent axis height}%+
%\pgfkeysvalueof{/pgfplots/group/vertical sep}
}
]
\addplot [draw=none] coordinates {(0,0)};
\end{axis}
\end{tikzpicture}
\end{document}
Result:
Any improvements ideas?
Best Answer
You simply need to adjust the height of the
color bar
plot.This can easily be accomplished by doing:
However, often you will use different heights for different groupplots. Therefore a more general approach would be:
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:
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>
andpoint meta max=<value>
. Please do remember this! :)Ok, so the final
groupplots
is something like this:This will result in: