我正在尝试绘制如下所示的函数:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{% normal distribution where #1 = mu and #2 = sigma
\pgfmathparse{exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}[every axis/.style={width=4cm}]
\begin{axis}[
name=axis6, %axis 6
xshift = 1.15in,
yshift = -2.5in,
no markers, domain=-0.7:0.7, samples=100,
%axis lines*=left,
%xlabel=$x$,
ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
%every axis x label/.style={at=(current axis.right of origin),anchor=west},
%height=5cm, width=12cm,
xtick={-0.7,0,0.7},
%xticklabels={-15, 0, 15},
%ytick={0 1},
ymin=0,
ymax=1.2,
enlargelimits=false, clip=false, axis on top,
%grid = major
]
\addplot [fill=gray!80, draw=none, domain=-0.7:-0.125] {gauss(0,.2)} \closedcycle;
\addplot [fill=gray!80, draw=none, domain=0.125:0.7] {gauss(0,.2)} \closedcycle;
\path[name path=axi] (axis cs:-0.15,0.75) -- (axis cs:0.15,0.75);
\path[name path=axj] (axis cs:-0.15,0) -- (axis cs:0.15,0);
%from this part things go bananas :/
% \addplot [
% thick,
% color=gray,
% fill=gray!80,
% %fill opacity=0.05
% ]
% fill between[
% of=axi and axj,
% %soft clip={domain=0:1},
% ];
%
% \path[name path=axk] (axis cs:-0.7,0.2) -- (axis cs:-0.35,0.2);
% \path[name path=axl] (axis cs:-0.7,0) -- (axis cs:-0.35,0);
% \addplot [
% thick,
% color=gray,
% fill=gray!20,
% %fill opacity=0.05
% ]
% fill between[
% of=axk and axl,
% %soft clip={domain=0:1},
% ];
\addplot [gray, dash dot] {gauss(-0.7,.2)};
\addplot [black, dashed] {gauss(-0.35,.2)};
\addplot [gray] {gauss(0,.2)};
\addplot [black!80] {gauss(0.35,.2)};
\addplot [black, dotted] {gauss(0.7,.2)};
\node (bar6a) at (0,1.4){};
\end{axis}
\end{tikzpicture}
\end{document}
这很好用,给了我:
但是,当我fill-between
将 的部分包含axis6
在图中时,我axis6
根本看不到 。我找不到这里出了什么问题:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{% normal distribution where #1 = mu and #2 = sigma
\pgfmathparse{exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}[every axis/.style={width=4cm}]
\begin{axis}[
name=axis6, %axis 6
xshift = 1.15in,
yshift = -2.5in,
no markers, domain=-0.7:0.7, samples=100,
%axis lines*=left,
%xlabel=$x$,
ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
%every axis x label/.style={at=(current axis.right of origin),anchor=west},
%height=5cm, width=12cm,
xtick={-0.7,0,0.7},
%xticklabels={-15, 0, 15},
%ytick={0 1},
ymin=0,
ymax=1.2,
enlargelimits=false, clip=false, axis on top,
%grid = major
]
\addplot [fill=gray!80, draw=none, domain=-0.7:-0.125] {gauss(0,.2)} \closedcycle;
\addplot [fill=gray!80, draw=none, domain=0.125:0.7] {gauss(0,.2)} \closedcycle;
\path[name path=axi] (axis cs:-0.15,0.75) -- (axis cs:0.15,0.75);
\path[name path=axj] (axis cs:-0.15,0) -- (axis cs:0.15,0);
%from this part things go bananas :/
\addplot [
thick,
color=gray,
fill=gray!80,
%fill opacity=0.05
]
fill between[
of=axi and axj,
%soft clip={domain=0:1},
];
%
% \path[name path=axk] (axis cs:-0.7,0.2) -- (axis cs:-0.35,0.2);
% \path[name path=axl] (axis cs:-0.7,0) -- (axis cs:-0.35,0);
% \addplot [
% thick,
% color=gray,
% fill=gray!20,
% %fill opacity=0.05
% ]
% fill between[
% of=axk and axl,
% %soft clip={domain=0:1},
% ];
\addplot [gray, dash dot] {gauss(-0.7,.2)};
\addplot [black, dashed] {gauss(-0.35,.2)};
\addplot [gray] {gauss(0,.2)};
\addplot [black!80] {gauss(0.35,.2)};
\addplot [black, dotted] {gauss(0.7,.2)};
\node (bar6a) at (0,1.4){};
\end{axis}
\end{tikzpicture}
\end{document}
什么都没给我。任何帮助都将不胜感激 :)
另一方面:它运作良好,
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{% normal distribution where #1 = mu and #2 = sigma
\pgfmathparse{exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}[every axis/.style={width=4cm}]
\begin{axis}[ %axis 2
name=axis2,
no markers, domain=-10:10, samples=100,
%axis lines*=left,
%xlabel=$x$,
ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
%every axis x label/.style={at=(current axis.right of origin),anchor=west},
%height=5cm, width=12cm,
xtick={-10,0,10},
%xticklabels={-15, 0, 15},
ytick=\empty,
ymin=0,
ymax=1.2,
enlargelimits=false, clip=false, axis on top,
%grid = major
]
\addplot [fill=gray!20, draw=none, domain=-10:-2.5] {gauss(0,3)} \closedcycle;
\addplot [fill=gray!20, draw=none, domain=2.5:10] {gauss(0,3)} \closedcycle;
\path[name path=axa] (axis cs:-2.5,0.7) -- (axis cs:2.5,0.7);
\path[name path=axb] (axis cs:-2.5,0) -- (axis cs:2.5,0);
\addplot [
thick,
color=gray,
fill=gray!20,
%fill opacity=0.05
]
fill between[
of=axa and axb,
%soft clip={domain=0:1},
];
%
\path[name path=axc] (axis cs:5,0.2) -- (axis cs:10,0.2);
\path[name path=axd] (axis cs:5,0) -- (axis cs:10,0);
\addplot [
thick,
color=black,
fill=black!40,
%fill opacity=0.05
]
fill between[
of=axc and axd,
%soft clip={domain=0:1},
];
\addplot [gray, dash dot] {gauss(-10,3)};
\addplot [black, dashed] {gauss(-5,3)};
\addplot [gray] {gauss(0,3)};
\addplot [black!80] {gauss(5,3)};
\addplot [black, dotted] {gauss(10,3)};
\node (bar2a) at (10,1) {};
\node (bar2b) at (10,0.2) {};
\end{axis}
\end{tikzpicture}
\end{document}
给我:
完整脚本:
%&lualatex
% !TeX TXS-program:compile = txs:///lualatex/[--shell-escape]
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{% normal distribution where #1 = mu and #2 = sigma
\pgfmathparse{exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}[every axis/.style={width=4cm}]
\begin{axis}[
name=axis1 %axis 1
no markers, domain=-10:10, samples=100,
%axis lines*=left,
%xlabel=$x$,
ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
%every axis x label/.style={at=(current axis.right of origin),anchor=west},
%height=5cm, width=12cm,
xtick={-10,0,10},
%xticklabels={-15, 0, 15},
%ytick={0 1},
ymin=0,
ymax=1.2,
enlargelimits=false, clip=false, axis on top,
%grid = major
]
\addplot [gray, dash dot] {gauss(-10,3)};
\addplot [black, dashed] {gauss(-5,3)};
\addplot [gray] {gauss(0,3)};
\addplot [black!80] {gauss(5,3)};
\addplot [black, dotted] {gauss(10,3)};
\addplot [color=black] coordinates { (2,0) (2,1.2) };
\node (bar1a) at (1,1){};
\node (bar1b) at (1,0.2){};
\end{axis}
\begin{axis}[ %axis 2
xshift = 1.15in,
name=axis2,
no markers, domain=-10:10, samples=100,
%axis lines*=left,
%xlabel=$x$,
ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
%every axis x label/.style={at=(current axis.right of origin),anchor=west},
%height=5cm, width=12cm,
xtick={-10,0,10},
%xticklabels={-15, 0, 15},
ytick=\empty,
ymin=0,
ymax=1.2,
enlargelimits=false, clip=false, axis on top,
%grid = major
]
\addplot [fill=gray!20, draw=none, domain=-10:-2.5] {gauss(0,3)} \closedcycle;
\addplot [fill=gray!20, draw=none, domain=2.5:10] {gauss(0,3)} \closedcycle;
\path[name path=axa] (axis cs:-2.5,0.7) -- (axis cs:2.5,0.7);
\path[name path=axb] (axis cs:-2.5,0) -- (axis cs:2.5,0);
\addplot [
thick,
color=gray,
fill=gray!20,
%fill opacity=0.05
]
fill between[
of=axa and axb,
%soft clip={domain=0:1},
];
%
\path[name path=axc] (axis cs:5,0.2) -- (axis cs:10,0.2);
\path[name path=axd] (axis cs:5,0) -- (axis cs:10,0);
\addplot [
thick,
color=black,
fill=black!40,
%fill opacity=0.05
]
fill between[
of=axc and axd,
%soft clip={domain=0:1},
];
\addplot [gray, dash dot] {gauss(-10,3)};
\addplot [black, dashed] {gauss(-5,3)};
\addplot [gray] {gauss(0,3)};
\addplot [black!80] {gauss(5,3)};
\addplot [black, dotted] {gauss(10,3)};
\node (bar2a) at (10,1) {};
\node (bar2b) at (10,0.2) {};
\end{axis}
\begin{axis}[ %axis 3
yshift = -1in,
name=axis3,
no markers, domain=-15:15, samples=100,
%axis lines*=left,
xlabel=$x$, ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
%every axis x label/.style={at=(current axis.right of origin),anchor=west},
%height=5cm, width=12cm,
xtick={-15, 0, 15},
%ytick={0 1},
enlargelimits=false, clip=false, axis on top,
%grid = major,
ymin=0,
ymax=1.2,
]
\addplot [gray, dash dot] {gauss(-15,3)};
\addplot [black, dashed] {gauss(-7.5,3)};
\addplot [gray] {gauss(0,3)};
\addplot [black!80] {gauss(7.5,3)};
\addplot [black, dotted] {gauss(15,3)};
%\addplot [cyan!50!black] {gauss(6.5,1)};
%\draw [yshift=-0.6cm, latex-latex](axis cs:4,0) -- node [fill=white] {$1.96\sigma$} (axis cs:5.96,0);
\addplot [color=black] coordinates { (-4,0) (-4,1.2) };
\node (bar3a) at (-4,1) {};
\node (bar3b) at (-4,0.2) {};
\end{axis}
\begin{axis}[ %axis 4
xshift = 1.15in,
yshift = -1in,
name=axis4,
no markers, domain=-15:15, samples=100,
%axis lines*=left,
%xlabel=$x$,
ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
%every axis x label/.style={at=(current axis.right of origin),anchor=west},
%height=5cm, width=12cm,
xtick={-15, 0, 15},
ytick=\empty,
ymin=0,
ymax=1.2,
enlargelimits=false, clip=false, axis on top,
%grid = major
]
\addplot [fill=black!30, draw=none, domain=-15:-10] {gauss(-7.5,3)} \closedcycle;
\addplot [fill=black!30, draw=none, domain=-5:15] {gauss(-7.5,3)} \closedcycle;
\addplot [fill=gray!20, draw=none, domain=5.3:15] {gauss(0,3)} \closedcycle;
\path[name path=axe] (axis cs:-10,0.7) -- (axis cs:-5,0.7);
\path[name path=axf] (axis cs:-10,0) -- (axis cs:-5,0);
\addplot [
thick,
color=black,
fill=black!30,
%fill opacity=0.05
]
fill between[
of=axe and axf,
%soft clip={domain=0:1},
];
%
\path[name path=axg] (axis cs:-5,0.2) -- (axis cs:5.3,0.2);
\path[name path=axh] (axis cs:-5,0) -- (axis cs:5.3,0);
\addplot [
thick,
color=gray,
fill=gray!20,
%fill opacity=0.05
]
fill between[
of=axg and axh,
%soft clip={domain=0:1},
];
\addplot [gray, dash dot] {gauss(-15,3)};
\addplot [black, dashed] {gauss(-7.5,3)};
\addplot [gray] {gauss(0,3)};
\addplot [black!80] {gauss(7.5,3)};
\addplot [black, dotted] {gauss(15,3)};
\node (bar4a) at (15,1) {};
\node (bar4b) at (15,0.2) {};
\node (bar4c) at (0,-0.2) {};
\end{axis}
\begin{axis}[
name=axis6, %axis 6
xshift = 1.15in,
yshift = -2.5in,
no markers, domain=-0.7:0.7, samples=100,
%axis lines*=left,
%xlabel=$x$,
ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
%every axis x label/.style={at=(current axis.right of origin),anchor=west},
%height=5cm, width=12cm,
xtick={-0.7,0,0.7},
%xticklabels={-15, 0, 15},
%ytick={0 1},
ymin=0,
ymax=1.2,
enlargelimits=false, clip=false, axis on top,
%grid = major
]
\addplot [fill=gray!80, draw=none, domain=-0.7:-0.125] {gauss(0,.2)} \closedcycle;
\addplot [fill=gray!80, draw=none, domain=0.125:0.7] {gauss(0,.2)} \closedcycle;
\path[name path=axi] (axis cs:-0.15,0.75) -- (axis cs:0.15,0.75);
\path[name path=axj] (axis cs:-0.15,0) -- (axis cs:0.15,0);
%from this part things go bananas :/ due to shifts :(
% \addplot [
% thick,
% color=gray,
% fill=gray!80,
% %fill opacity=0.05
% ]
% fill between[
% of=axi and axj,
% %soft clip={domain=0:1},
% ];
%
% \path[name path=axk] (axis cs:-0.7,0.2) -- (axis cs:-0.35,0.2);
% \path[name path=axl] (axis cs:-0.7,0) -- (axis cs:-0.35,0);
% \addplot [
% thick,
% color=gray,
% fill=gray!20,
% %fill opacity=0.05
% ]
% fill between[
% of=axk and axl,
% %soft clip={domain=0:1},
% ];
\addplot [gray, dash dot] {gauss(-0.7,.2)};
\addplot [black, dashed] {gauss(-0.35,.2)};
\addplot [gray] {gauss(0,.2)};
\addplot [black!80] {gauss(0.35,.2)};
\addplot [black, dotted] {gauss(0.7,.2)};
\node (bar6a) at (0,1.4){};
\end{axis}
\draw [color=black,dash dot] (bar1a) -- (bar2a);
\draw [color=black,dash dot] (bar1b) -- (bar2b);
\draw [color=black,dash dot] (bar3a) -- (bar4a);
\draw [color=black,dash dot] (bar3b) -- (bar4b);
\path [->, draw, color=black] (bar4c) -- node [midway,left] {something}(bar6a);
% \draw [color=black,dash dot] (bar2) -- (bar3);
% \draw [color=black,dash dot] (bar3) -- (bar4);
% \draw [->,thick,color=red,dashed] (bar3) -- (bar4);
% \begin{scope}[every node/.style={text width=7cm,align=left}]
% \node [below] at (axis1.south) {\captionof{figure}{First caption}};
% \node [below] at (axis2.south) {\captionof{figure}{Second caption}};
% \node [below] at (axis3.south) {\captionof{figure}{Third caption}};
% \end{scope}
\end{tikzpicture}
\end{document}
答案1
正如在问题下方评论移位 ( xshift
/ yshift
) 造成了混乱。假设您使用它们将几个axis
环境对齐并叠放在一起,我建议使用正确的at
andanchor
语句。这给出了附加的结果。
它遵循重构后的代码,比起始代码干净得多,并在很多地方使用了样式。有关详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
% use this `compat' level or higher to make use of Lua when compiling
% with lualatex. This `compat' level also makes `axis cs:' the default
% coordinate system for TikZ coordinates
compat=1.12,
% declare some constants and the Gauss function for later use
/pgf/declare function={
ymin = 0;
ymax = 1.2;
%
FillHeight = 0.7;
%
LowerLine = 0.2;
UpperLine = 1.0;
%
xVertBarAxis1 = 2;
xVertBarAxis3 = -4;
%
gauss(\mu,\sigma) = exp(-((x-\mu)^2)/(2*\sigma^2));
},
% define a custom style for the `axis' so we don't have to repeat all
% the options over and over again. Of course this also makes it much
% easier to change the style of these plots
my axis style/.style={
ylabel=$y$,
ymin=ymin,
ymax=ymax,
samples=101,
enlargelimits=false,
axis on top,
% use the custom created cycle list here
cycle list name=my cycle list,
every axis y label/.style={
at=(current axis.above origin),
anchor=south,
},
% simply add the `xaxis' path to every plot which uses this style
% (so again it is not needed to repeat this)
execute at begin axis={
\path [name path=xaxis]
(\pgfkeysvalueof{/pgfplots/xmin},0) --
(\pgfkeysvalueof{/pgfplots/xmax},0)
;
},
% use `extra x ticks' to add the vertical lines to the plots ...
% (the `#1' means that an optional argument to this style will
% be forwarded here)
extra x ticks={#1},
% ... and this is the style which should be applied to the extra ticks
extra x tick style={
xticklabels={},
major tick length=0pt,
grid=major,
grid style={
draw=black,
},
},
},
% define some more custom styles for later use
my light gray fill/.style={
draw=none,
fill=gray!20,
},
my dark gray fill/.style={
draw=none,
fill=black!40,
},
}
% create a custom `cycle list' so that one don't has to repeat this
% over and over again
\pgfplotscreateplotcyclelist{my cycle list}{
gray,dash dot\\
black,dashed\\
gray\\
black!80\\
black,dotted\\
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% name this plot so the others can be placed relative to this one
name=axis1,
% use the (optional) argument of the custom style
% to place the vertical line at the given position
my axis style={2},
xtick={-10,0,10},
domain=-10:10,
]
% with the custom `cycle list' this can be simplified to
\foreach \i in {-10,-5,...,10} {
\addplot {gauss(\i,3)};
}
% then here it only remains to place the dummy coordinates for the
% horizontal lines, which will be drawn after all `axis' environments
\begin{scope}[
xshift=-2.5mm,
]
\coordinate (bar1a) at (xVertBarAxis1,LowerLine);
\coordinate (bar1b) at (xVertBarAxis1,UpperLine);
\end{scope}
\end{axis}
\begin{axis}[
name=axis2,
% ---------------------------------------------------------------------
my axis style,
% place the other `axis' environments relative to the previous ones
at={(axis1.right of east)},
anchor=left of west,
% ---------------------------------------------------------------------
xtick={-10,0,10},
ytick=\empty,
domain=-10:10,
]
\foreach \i in {-10,-5,...,10} {
\addplot {gauss(\i,3)};
}
% this is one way of adding the fill
% later it will be shown another one
% ----------------------------------
% put this fill on the same layer as the `fill between' stuff
\pgfonlayer{pre main}
% use the custom style here
\addplot [my dark gray fill] coordinates {
(0,LowerLine)
(10,LowerLine)
}
\closedcycle
;
\endpgfonlayer
% here I create a dummy path which is than used by `fill between'
% (optimally this is not needed, but `soft clip={domain y=0:FillHeight}'
% did some strange things)
\addplot [
draw=none,
name path=gauss,
] {min(FillHeight, gauss(0,3))};
\addplot [my light gray fill] fill between [
of=gauss and xaxis,
];
% this time we need to place coordinates for the horizontal lines
% at the maximum x values, which are not set explicitly. But we can
% get them with the help of `\pgfkeysvalueof{...}'.
\coordinate (bar2a) at (\pgfkeysvalueof{/pgfplots/xmax},LowerLine);
\coordinate (bar2b) at (\pgfkeysvalueof{/pgfplots/xmax},UpperLine);
\end{axis}
\begin{axis}[
name=axis3,
% ---------------------------------------------------------------------
my axis style={-4},
at={(axis1.below south)},
anchor=above north,
% ---------------------------------------------------------------------
xlabel=$x$,
xtick={-15,0,15},
domain=-15:15,
]
\foreach \i in {-15,-7.5,...,15} {
\addplot {gauss(\i,3)};
}
\begin{scope}[
xshift=-5mm,
]
\coordinate (bar3a) at (xVertBarAxis3,LowerLine);
\coordinate (bar3b) at (xVertBarAxis3,UpperLine);
\end{scope}
\end{axis}
\begin{axis}[
name=axis4,
% ---------------------------------------------------------------------
my axis style,
at={(axis3.right of east)},
anchor=left of west,
% ---------------------------------------------------------------------
xtick={-15,0,15},
ytick=\empty,
domain=-15:15,
]
\foreach \i in {-15,-7.5,...,15} {
\addplot {gauss(\i,3)};
}
\addplot [
draw=none,
name path=gauss1,
samples=201,
] {min(LowerLine, gauss(0,3))};
\addplot [my light gray fill] fill between [
of=gauss1 and xaxis,
];
\addplot [
draw=none,
name path=gauss2,
samples=201,
] {min(FillHeight, gauss(-7.5,3))};
\addplot [my dark gray fill] fill between [
of=gauss2 and xaxis,
];
\coordinate (bar4a) at (\pgfkeysvalueof{/pgfplots/xmax},LowerLine);
\coordinate (bar4b) at (\pgfkeysvalueof{/pgfplots/xmax},UpperLine);
\end{axis}
\begin{axis}[
name=axis6,
% ---------------------------------------------------------------------
my axis style,
at={(axis4.below south)},
% because there needs to be some more space for the arrow between
% the plots, we increase the gab
yshift=-10mm,
anchor=above north,
% ---------------------------------------------------------------------
xtick={-0.7,0,0.7},
domain=-0.7:0.7,
]
\foreach \i in {-0.7,-0.35,...,0.7} {
\addplot {gauss(\i,0.2)};
}
\pgfonlayer{pre main}
\addplot [my light gray fill] coordinates {
(-0.7,LowerLine)
(0,LowerLine)
}
\closedcycle
;
\endpgfonlayer
\addplot [
draw=none,
name path=gauss,
samples=201,
] {min(0.75, gauss(0,0.2))};
\addplot [my dark gray fill] fill between [
of=gauss and xaxis,
];
\end{axis}
% draw the horizontal lines between the upper and middle row of plots
\draw [dash dot]
(bar1a) -- (bar2a)
(bar1b) -- (bar2b)
(bar3a) -- (bar4a)
(bar3b) -- (bar4b)
;
% draw the arrow between the lower right two plots
\draw [->]
% if you want to, you can shift it a bit, as shown here using the
% optional argument of the coordinate
([xshift=10mm] axis6.above north)
-- node [left] {something}
([xshift=10mm] axis4.below south)
;
\end{tikzpicture}
\end{document}