我正在尝试制作一个直方图,并在其上绘制拟合曲线。这是 mwe:
\documentclass[a4paper]{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\pgfplotsset{compat=1.10}
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{2pt}
\newcommand{\plots}{0.611201}
\newcommand{\plotm}{2.19882}
\begin{filecontents}{data.dat}
2 0.0629921259843
3 0.0236220472441
4 0.0314960629921
5 0.125984251969
6 0.0629921259843
7 0.102362204724
8 0.110236220472
9 0.0551181102362
10 0.0629921259843
11 0.0314960629921
12 0.0236220472441
13 0.0314960629921
14 0.0629921259843
15 0.0551181102362
16 0.0393700787402
17 0.0472440944882
18 0.00787401574803
19 0.0393700787402
24 0.00787401574803
27 0.00787401574803
33 0.00787401574803
\end{filecontents}
\begin{document}
\begin{preview}
\begin{tikzpicture}
\begin{axis}[
yticklabel style={/pgf/number format/fixed},
scaled y ticks = false,
minor y tick num={1},
xtick pos=left,
legend cell align = left,
legend style={draw=none},
xlabel = {group size},
ylabel = {ratio}
]
\addplot[blue,ybar,fill, fill opacity=0.3, bar width = 0.8,] table {data.dat};
\addplot[red, line width = 1,domain=1:40,samples=100] {1/(x*sqrt(2*pi)*\plots)*exp(-(ln(x)-\plotm)^2/(2*\plots^2))};
\legend{empirical,lognormal fit}
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}
如您所见,图例效果不太好。我该如何更改图例,使其看起来更像这样?
答案1
有几种方法可以解决这个问题。一种方法是将选项ybar
从\addplot
移到axis
。同一个键在两个位置的含义略有不同,在 它axis
指的是/pgfplots/ybar
,而在 它\addplot
指的是/tikz/ybar
。这似乎会导致图例的不同行为,尽管我还没有研究它们是如何定义的。
当然,当你添加ybar
到 时axis
,它会影响所有的\addplot
,所以你还必须添加sharp plot
到线图中。sharp plot
是 的默认样式addplot
,它默认用标记画一条线。
结果和完整代码如下:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\pgfplotsset{compat=1.10}
\newcommand{\plots}{0.611201}
\newcommand{\plotm}{2.19882}
\begin{filecontents}{data.dat}
2 0.0629921259843
3 0.0236220472441
4 0.0314960629921
5 0.125984251969
6 0.0629921259843
7 0.102362204724
8 0.110236220472
9 0.0551181102362
10 0.0629921259843
11 0.0314960629921
12 0.0236220472441
13 0.0314960629921
14 0.0629921259843
15 0.0551181102362
16 0.0393700787402
17 0.0472440944882
18 0.00787401574803
19 0.0393700787402
24 0.00787401574803
27 0.00787401574803
33 0.00787401574803
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
yticklabel style={/pgf/number format/fixed},
scaled y ticks = false,
minor y tick num={1},
xtick pos=left,
legend cell align = left,
legend style={draw=none},
xlabel = {group size},
ylabel = {ratio},
ybar
]
\addplot[blue,ybar,fill, fill opacity=0.3, bar width = 0.8] table {data.dat};
\addplot[sharp plot,red, line width = 1,domain=1:40,samples=100,line legend] {1/(x*sqrt(2*pi)*\plots)*exp(-(ln(x)-\plotm)^2/(2*\plots^2))};
\legend{empirical,lognormal fit}
\end{axis}
\end{tikzpicture}
\end{document}
第二种选择
另一种方法是简单地添加到绘图命令area legend
的选项中。这告诉创建一个填充的矩形作为图例条目,而不是像首选示例中那样只是一条线。您也可以使用而不是,这将使其像第一个示例中那样。您可以在手册中找到有关如何创建图例条目的定义。\addplot
ybar
pgfplots
ybar legend
area legend
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\pgfplotsset{compat=1.10}
\newcommand{\plots}{0.611201}
\newcommand{\plotm}{2.19882}
\begin{filecontents}{data.dat}
2 0.0629921259843
3 0.0236220472441
4 0.0314960629921
5 0.125984251969
6 0.0629921259843
7 0.102362204724
8 0.110236220472
9 0.0551181102362
10 0.0629921259843
11 0.0314960629921
12 0.0236220472441
13 0.0314960629921
14 0.0629921259843
15 0.0551181102362
16 0.0393700787402
17 0.0472440944882
18 0.00787401574803
19 0.0393700787402
24 0.00787401574803
27 0.00787401574803
33 0.00787401574803
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
yticklabel style={/pgf/number format/fixed},
scaled y ticks = false,
minor y tick num={1},
xtick pos=left,
legend cell align = left,
legend style={draw=none},
xlabel = {group size},
ylabel = {ratio}
]
\addplot[blue,ybar,fill, fill opacity=0.3, bar width = 0.8,area legend] table {data.dat};
\addplot[red, line width = 1,domain=1:40,samples=100] {1/(x*sqrt(2*pi)*\plots)*exp(-(ln(x)-\plotm)^2/(2*\plots^2))};
\legend{empirical,lognormal fit}
\end{axis}
\end{tikzpicture}
\end{document}
第三种选择(处理对齐问题)
可以看出,在第一个选项中,直方图的图例未对齐。我们可以通过覆盖的默认行为来解决这个问题ybar legend
。默认行为(参见手册)是
\pgfplotsset{
/pgfplots/xbar legend/.style={
/pgfplots/legend image code/.code={%
\draw[##1,/tikz/.cd,bar width=3pt,yshift=-0.2em,bar shift=0pt]
plot coordinates {(0cm,0.8em) (2*\pgfplotbarwidth,0.6em)};},
},
}
下面是一个完整代码的示例,看起来更加美观ybar legend
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\pgfplotsset{compat=1.10}
\newcommand{\plots}{0.611201}
\newcommand{\plotm}{2.19882}
\begin{filecontents}{data.dat}
2 0.0629921259843
3 0.0236220472441
4 0.0314960629921
5 0.125984251969
6 0.0629921259843
7 0.102362204724
8 0.110236220472
9 0.0551181102362
10 0.0629921259843
11 0.0314960629921
12 0.0236220472441
13 0.0314960629921
14 0.0629921259843
15 0.0551181102362
16 0.0393700787402
17 0.0472440944882
18 0.00787401574803
19 0.0393700787402
24 0.00787401574803
27 0.00787401574803
33 0.00787401574803
\end{filecontents}
\pgfplotsset{
/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={%
\draw[##1,/tikz/.cd,bar width=0.1cm,yshift=-0.2em,bar shift=0.5*\pgfplotbarwidth]
plot coordinates {(0.5*\pgfplotbarwidth,0.6em) (2.5*\pgfplotbarwidth,0.4em) (4.5*\pgfplotbarwidth,0.2em)};},
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
yticklabel style={/pgf/number format/fixed},
scaled y ticks = false,
minor y tick num={1},
xtick pos=left,
legend cell align = left,
legend style={draw=none},
xlabel = {group size},
ylabel = {ratio}
]
\addplot[blue,ybar,fill, fill opacity=0.3, bar width = 0.8,ybar legend] table {data.dat};
\addplot[red, line width = 1,domain=1:40,samples=100] {1/(x*sqrt(2*pi)*\plots)*exp(-(ln(x)-\plotm)^2/(2*\plots^2))};
\legend{empirical,lognormal fit}
\end{axis}
\end{tikzpicture}
\end{document}