我正在尝试使用下面的代码在 tikz 图片中的 3.0283 处添加一条红色水平虚线。到目前为止,我尝试过的选项要么没有编译,要么在 (0,3.0283) 处画了一条虚线。任何建议都非常感谢!提前谢谢您。
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{caption}
\usepackage{wrapfig}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.15}
\begin{document}
\begin{figure}
\centering
\pgfplotsset{width=17cm}
\begin{tikzpicture}
\begin{semilogxaxis}[log basis x=2, xmin=2^4, xmax=2^18, ymin=1.5, ymax= 4.3, xlabel={Number Iterations}, ylabel={Expected Value of European Call}]
\addplot+[only marks, color=black, mark options={fill=black}, error bars/.cd, y dir=both,y explicit]
coordinates {
(2^5, 2.575426873) +- (0,0.879134009)
(2^6, 3.248958815) +- (0,0.878972497)
(2^7, 2.777192212) +- (0,0.538995365)
(2^8, 3.087205046) +- (0,0.430526707)
(2^9, 2.839813099) +- (0,0.271699898)
(2^10, 2.99962185) +- (0,0.200001514)
(2^11, 3.036591208) +- (0,0.142075497)
(2^12, 2.995606084) +- (0,0.101353885)
(2^13, 3.019666878) +- (0,0.069387224)
(2^14, 3.079018363) +- (0,0.050965547)
(2^15, 3.001740643) +- (0,0.035138634)
(2^16, 3.029783754) +- (0,0.025229816)
(2^17, 3.027393515) +- (0,0.017812922)
};
\addplot[black,dashed]coordinates {(0,3.0283)--(2^18,3.0283)};
\end{semilogxaxis}
\end{tikzpicture}
\caption{Estimations with $95\%$ Confidence Intervals}
\label{errors}
\end{figure}
\end{document}
答案1
您可以只添加一个常数\addplot[black,dashed,domain = 2^4:2^18,samples = 2] {3.0283};
。
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{caption}
\usepackage{wrapfig}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.15}
\begin{document}
\begin{figure}
\centering
\pgfplotsset{width=17cm}
\begin{tikzpicture}
\begin{semilogxaxis}[log basis x=2, xmin=2^4, xmax=2^18, ymin=1.5, ymax= 4.3, xlabel={Number Iterations}, ylabel={Expected Value of European Call}]
\addplot+[only marks, color=black, mark options={fill=black}, error bars/.cd, y dir=both,y explicit]
coordinates {
(2^5, 2.575426873) +- (0,0.879134009)
(2^6, 3.248958815) +- (0,0.878972497)
(2^7, 2.777192212) +- (0,0.538995365)
(2^8, 3.087205046) +- (0,0.430526707)
(2^9, 2.839813099) +- (0,0.271699898)
(2^10, 2.99962185) +- (0,0.200001514)
(2^11, 3.036591208) +- (0,0.142075497)
(2^12, 2.995606084) +- (0,0.101353885)
(2^13, 3.019666878) +- (0,0.069387224)
(2^14, 3.079018363) +- (0,0.050965547)
(2^15, 3.001740643) +- (0,0.035138634)
(2^16, 3.029783754) +- (0,0.025229816)
(2^17, 3.027393515) +- (0,0.017812922)
};
\addplot[black,dashed,domain = 2^4:2^18,samples = 2] {3.0283};
\end{semilogxaxis}
\end{tikzpicture}
\caption{Estimations with $95\%$ Confidence Intervals}
\label{errors}
\end{figure}
\end{document}
注意:由于对数轴,如果您尝试从 开始,则会导致错误x = 0
。