如何在直方图中添加线条?

如何在直方图中添加线条?

我真的很感激到目前为止我得到的帮助,所以我再问一个问题。:)

我在 Overleaf 中制作了一个直方图,我对它很满意。但缺少两件事——像这样的两行:

在此处输入图片描述 在此处输入图片描述

我该如何添加这些?提前致谢!

我的代码(相当混乱):

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}
\pgfplotsset{width=7.5cm,compat=1.12}
\usepgfplotslibrary{fillbetween}
\usepackage{pgfplotstable}
\usepackage{subfig}
\pgfplotsset{compat=1.7}
\usepackage[section]{placeins}
\usepackage[labelfont=bf]{caption}
\usepackage{siunitx}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[font=small, labelfont=bf]{caption}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\usepackage[margin=25mm]{geometry}
\tolerance=1
\emergencystretch=\maxdimen
\hyphenpenalty=10000
\hbadness=10000
\setstretch{1.3}
\renewcommand{\contentsname}{Table of Contents}
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,bindingoffset=6mm]{geometry}

\begin{document}
\begin{figure}
    \centering
\caption{Negative Campaigning in the Swedish Multi-Party System 1970-2022.}
\begin{tikzpicture}
\begin{axis}[x tick label style={/pgf/number format/.cd,%
        scaled x ticks = false,
        set thousands separator={},
        fixed}, yticklabel={$\pgfmathprintnumber{\tick}\%$}, grid=both, minor tick num=9, grid style={line width=.2pt, draw=black!10}, major grid style={line width=.4pt, draw=black!20}, enlargelimits={abs=0.1}, width=12cm, height=8cm, xmin=1970, xmax=2022, ymin=0, ymax=16, xlabel= \textbf{Time}, ylabel= \textbf{Degree of Negative Campaigning}, xtick={1970,1973,1976,1979,1982,1985,1988,1991,1994,1998,2002,2006,2010,2014,2018,2022}, every tick label/.append style={font=\tiny}] 
\addplot+[black, ybar interval, mark=no,fill = lightgray] plot coordinates {
(1970,13.6363658) 
(1973,3.4666667)
(1976,9.4786733)
(1979,7.7683621)
(1982,14.6520151)
(1985,11.3144759)
(1988,1.8472911)
(1991,4.14201161)
(1994,5.5229145)
(1998,3.23846942)
(2002,4.75448152)
(2006,3.7750991)
(2010,3.43)
(2014,5.74) 
(2018,7.05) 
(2022,6.5)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

答案1

您的问题和评论包含的信息不充分且相互矛盾,因此您需要根据您的数据调整此答案。

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
declare function={
 mean=1987;
 variance=10;
 gauss(\x)=1/(variance*sqrt(2*pi))*exp(-((\x-mean)^2)/(2*variance^2));
}]
\begin{axis}[
x tick label style={/pgf/number format/.cd, scaled x ticks=false, set thousands separator={}, fixed},
yticklabel={$\pgfmathprintnumber{\tick}\%$},
grid=both,
minor tick num=9,
grid style={line width=.2pt, draw=black!10},
major grid style={line width=.4pt, draw=black!20},
enlargelimits={abs=0.1},
width=12cm,
height=8cm,
xmin=1970, xmax=2022,
ymin=0, ymax=16,
xlabel=\textbf{Time},
ylabel=\textbf{Degree of Negative Campaigning},
xtick={1970,1973,1976,1979,1982,1985,1988,1991,1994,1998,2002,2006,2010,2014,2018,2022},
every tick label/.append style={font=\tiny}
]
\addplot+[black, ybar interval, mark=none, fill=lightgray] plot coordinates {
(1970,13.6363658) 
(1973,3.4666667)
(1976,9.4786733)
(1979,7.7683621)
(1982,14.6520151)
(1985,11.3144759)
(1988,1.8472911)
(1991,4.14201161)
(1994,5.5229145)
(1998,3.23846942)
(2002,4.75448152)
(2006,3.7750991)
(2010,3.43)
(2014,5.74) 
(2018,7.05) 
(2022,6.5)};
\draw[red, ultra thick] (mean,0 |-current axis.above origin) --node[right]{Mean} (mean,0 |-current axis.below origin);
\addplot[red, ultra thick, domain=1970:2022, samples=100] (x,{100*gauss(x)});
\end{axis}
\end{tikzpicture}
\end{document}

带线条和钟形曲线的条形图

相关内容