You have accidentally generated a mesh plot instead of a line plot.
The command \addplot3
always samples a matrix of data values. If you want it to sample just a line, you have to say samples y=1
. Consequently, the line plot command needs to be adopted as follows:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\pgfplotsset{
colormap={whitered}{color(0cm)=(white); color(1cm)=(orange!75!red)}
}
\begin{tikzpicture}[
scale=1,
declare function={fgm(\theta)=1+\theta*(1-2*x)*(1-2*y);},
declare function={fgmct(\theta,\val)=1+\theta*(1-2*x)*(1-2*(\val-x));}]
\begin{axis}[
colormap name=whitered,
width=15cm,
view={-45}{60},
enlargelimits=false,
disabledatascaling,
grid=major,
domain=0:1,
y domain=0:1,
samples=30,
xlabel=$x_1$,
ylabel=$x_2$,
zlabel={$f(x_1,x_2)$},
colorbar horizontal,
colorbar style={
at={(0,0.9)},
anchor=south west,
width=0.25*\pgfkeysvalueof{/pgfplots/parent axis width},
title={$f(x_1,x_2)$}
}
]
\addplot3 [surf] {fgm(-0.5)};
\addplot3 [domain=0:0.8, samples y=1, black!70,smooth] (x, 0.8-x, {fgmct(-0.5,0.8)});
\end{axis}
\end{tikzpicture}
\end{document}
You see that I removed the y domain
and I set samples y=1
. This samples only in x
.
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}
Best Answer
The default surface plot of
pgfplots
uses two triangles for each rectangular patch segment. Usually, the diagonal does not matter much -- but in this case, it really matters and the result is unsuitable.Note that
shader=interp
appears to select the other diagonal (unintentionally, but it does). A simple solution would be to addshader=interp
, unless you really need the grid lines.