我有这个数字:
连续线是绘制的高斯函数。我想用我的数据点(即红色虚线)替换该函数,并使其看起来一样。意思是有一条连续的线,并在下方填充颜色。不幸的是,我不知道该怎么做。有人能帮忙吗?(抱歉再次发帖,我曾尝试自己解决这个问题,但没有成功)谢谢!
我的代码:
\usepackage{tikz}
\usepackage{biblatex}
\addbibresource{biblatex-examples.bib}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{datax.dat}
x,y
-1.9,0.001
-1.8,0.001
-1.7,0.002
-1.6,0.001
-1.5,0.01
-1.4,0.004
-1.3,0.005
-1.2,0.008
-1.1,0.01
-1,0.014
-0.9,0.029
-0.8,0.037
-0.7,0.056
-0.6,0.119
-0.5,0.187
-0.4,0.391
-0.3,0.667
-0.2,1.062
-0.1,1.648
0,2.418
0.1,2.454
0.2,1.829
0.3,1.243
0.4,0.809
0.5,0.537
0.6,0.319
0.7,0.178
0.8,0.101
0.9,0.06
1,0.035
1.1,0.019
1.2,0.018
1.3,0.014
1.4,0.019
1.5,0.004
1.6,0.004
1.7,0.002
1.8,0.003
1.9,0.001
2,0.003
2.1,0.001
2.2,0.002
2.3,0.001
2.4,0.001
\end{filecontents}
\usetikzlibrary{datavisualization}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
/pgf/declare function={
% normal distribution where \mean = mean and \stddev = sd}
gauss(\mean,\stddev) = 1/sqrt(2*pi*\stddev^2) * exp(-((\x-\mean)^2)/(2*\stddev^2));
% define xmin and xmax only once here and reuse them later
xmin=-2;
xmax=2;
},
}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=middle,
axis on top,
xmin=xmin,
xmax=xmax,
domain=xmin:xmax,
samples=100,
]
\path[name path=axis] (axis cs:xmin,0) -- (axis cs:xmax,0);
\addplot [
thick,
name path=table,
] {gauss(0.1, 0.25)};
\addplot table [
x index=0,
y index=1,
col sep=comma,
name path=table
] {datax.dat};
% instead of using a function here you could also give table values or read a (table) file
% \addplot table {<table> or <file name>};
\addplot [
fill=red!10,
] fill between [
of=axis and table,
];
\end{axis}
\end{tikzpicture}
\end{document}
答案1
问题是您将名称路径添加到了表格选项中,而不是添加到了绘图中。这些选项会影响绘图,而不是表格,因此应该添加到绘图中。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{datax.dat}
x,y
-1.9,0.001
-1.8,0.001
-1.7,0.002
-1.6,0.001
-1.5,0.01
-1.4,0.004
-1.3,0.005
-1.2,0.008
-1.1,0.01
-1,0.014
-0.9,0.029
-0.8,0.037
-0.7,0.056
-0.6,0.119
-0.5,0.187
-0.4,0.391
-0.3,0.667
-0.2,1.062
-0.1,1.648
0,2.418
0.1,2.454
0.2,1.829
0.3,1.243
0.4,0.809
0.5,0.537
0.6,0.319
0.7,0.178
0.8,0.101
0.9,0.06
1,0.035
1.1,0.019
1.2,0.018
1.3,0.014
1.4,0.019
1.5,0.004
1.6,0.004
1.7,0.002
1.8,0.003
1.9,0.001
2,0.003
2.1,0.001
2.2,0.002
2.3,0.001
2.4,0.001
\end{filecontents}
\usetikzlibrary{datavisualization}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
/pgf/declare function={
% normal distribution where \mean = mean and \stddev = sd}
gauss(\mean,\stddev) = 1/sqrt(2*pi*\stddev^2) * exp(-((\x-\mean)^2)/(2*\stddev^2));
% define xmin and xmax only once here and reuse them later
xmin=-2;
xmax=2;
},
}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=middle,
axis on top,
xmin=xmin,
xmax=xmax,
domain=xmin:xmax,
samples=100,
]
\path[name path=axis] (axis cs:xmin,0) -- (axis cs:xmax,0);
\addplot [
thick,
%name path=table,
] {gauss(0.1, 0.25)};
\addplot[name path=table,mark=square*,color=red] table [
x index=0,
y index=1,
col sep=comma,
] {datax.dat};
% instead of using a function here you could also give table values or read a (table) file
% \addplot table {<table> or <file name>};
\addplot [
fill=red!10,
] fill between [
of=axis and table,
];
\end{axis}
\end{tikzpicture}
\end{document}