我想要创建一个如下的图表:
但是,我已成功创建图表,但失败了:
- 将蓝色箭头添加到我的图表中,
- 在曲线和轴箭头之间添加一些空间,然后
- 增加整个 tikzpicture 的尺寸,当添加到书籍、文章文档类时,图形非常小。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{ticks=none}
\begin{axis}[
scaled ticks=false,
xmin=0,
ymin=0,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.05)},anchor=north},
y label style={at={(axis description cs:-0.05,.5)},rotate=90,anchor=south},
xlabel=Number of stocks in portfolio,
ylabel=Portfoilio Risk,
]
\addplot[domain=0:10, red, ultra thick,smooth] {e^(-.9*x)+.5};
\addplot[domain=0:10, gray, dashed,thick] {1.5};
\addplot[domain=0:10, gray,dashed,thick] {.5};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这不是更好的解决方案(我更喜欢只使用 tikz 包)。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1.5]
\pgfplotsset{ticks=none}
\begin{axis}[
scaled ticks=false,
xmin=0,
ymin=0,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.05)},anchor=north},
y label style={at={(axis description cs:-0.05,.5)},rotate=90,anchor=south},
xlabel=Number of stocks in portfolio,
ylabel=Portfoilio Risk,
]
\addplot[domain=0:10, red, ultra thick,smooth] {e^(-.9*x)+.5};
\addplot[domain=0:10, gray, dashed,thick] {1.5};
\addplot[domain=0:10, gray,dashed,thick] {.5};
\end{axis}
\draw[latex-latex, gray, line width=5pt] (1.2,0)--(1.2,5.7) node[black,pos=.3,fill=white] () {\large\bfseries Total stock risk};
\draw[latex-latex, gray, line width=5pt] (5,0)--(5,1.9) node[black,pos=.5,fill=white] () {\large\bfseries Undiversifiable Market Risk (Systematic Risk)};
\draw[-latex,gray,line width=5pt] (2,5.7)--(2,2.2) node[black,pos=.5,fill=white,right] () {\large\bfseries Unsystematic Risk eliminated by diversification};;
\end{tikzpicture}
\end{center}
\end{document}
输出:
添加:仅限 tikz 版本。代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[yscale=10]
\draw[thick,->] (0,0)--(12,0) node[pos=.5,below] () {\large\bfseries Number of stocks in portfolio};
\draw[thick,->] (0,0)--(0,1.7) node[midway,left] () {\large\bfseries \rotatebox{90}{Portfoilio Risk}};
\draw[red,line width=2pt] plot[domain=0:12,smooth] (\x,{e^(-.9*\x)+.5});
\draw[blue,dashed] (0,1.5)--(12,1.5);
\draw[blue,dashed] (0,.5)--(12,.5);
\draw[latex-latex, gray, line width=5pt] (2,0)--(2,1.5) node[black,pos=.3,fill=white] () {\large\bfseries Total stock risk};
\draw[latex-latex, gray, line width=5pt] (8,0)--(8,.5) node[black,pos=.5,fill=white] () {\large\bfseries Undiversifiable Market Risk (Systematic Risk)};
\draw[-latex,gray,line width=5pt] (4,1.5)--(4,.53) node[black,pos=.5,fill=white,right,align=left] () {\large\bfseries Unsystematic Risk eliminated\\\large \bfseries by diversification};;
\end{tikzpicture}
\end{center}
\end{document}
输出:
答案2
我不太清楚pgfplots
,我试过了……
我创建了一个带有 2 个参数的命令,用于将文本放置在曲线上。我使用了\fpeval
来自xfp
包
编辑:
- 我修改了
\scale=1.5
以\width=0.75\linewidth
避免标签也被放大 - 我纠正一个错误
\fpeval{exp{-#1*0.9}+.5}
,错了,要加上括号!
编辑2
Hasan Basri Çetin 对这篇文章的回答从 x 到 f(x) 画一条垂直线,这个
xfp
包没什么用,我们可以使用以下方法在曲线上定位一个点:declare function ={myfun(\x) = e^(-.9*\x)+.5;},domain=0:10
等(axis cs: 3, {myfun(3)})
- 我们可以用来
\pgfplotsinvokeforeach
绘制虚线段
代码
\documentclass{article}
%https://tex.stackexchange.com/questions/680359/adding-labelling-arrows-to-a-plot
\usepackage{showframe}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}%<-- added
\usetikzlibrary {arrows.meta}%<-- added
\usepackage{tikz}
\begin{document}
\newcommand{\mycalc}[2]{%
\draw[myarrow,->,shorten >=1pt](axis cs: #1,1.5) -- node[mytext,right]{#2} (axis cs: #1, {myfun(#1)});
}
\begin{center}
\begin{tikzpicture}[%scale=1.5,
%https://tex.stackexchange.com/questions/680588/draw-a-vertical-line-from-x-to-fx
declare function ={myfun(\x) = e^(-.9*\x)+.5;},domain=0:10,
myarrow/.style = {<->,>={Latex[length=2mm, angle=90:10pt]},blue, line width=4pt},
mytext/.style = {black,pos=.5,fill=white,font=\bfseries\small},
]
\pgfplotsset{ticks=none}
\begin{axis}[
width=0.75\linewidth,%<-- to replace scale=1.5
xmin=0,
ymin=0,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.05)},anchor=north,%font=\bfseries\large% we can
},
y label style={at={(axis description cs:-0.05,.5)},rotate=90,anchor=south},
xlabel=Number of stocks in portfolio,
ylabel=Portfoilio Risk,
]
\pgfplotsinvokeforeach{0.5,1.5}{
\draw [dashed] (axis cs:0,#1) -- (axis cs:10,#1);
}
% \addplot[domain=0:10, red, ultra thick,smooth] {e^(-.9*x)+.5};
\addplot[red, ultra thick,smooth] {myfun(x)};
\draw[myarrow](axis cs: 1.5,1.5) --node[mytext,pos=0.75,xshift=5mm]{Total Stock Risk} (axis cs: 1.5,0);
\draw[myarrow](7.5,0.5) -- node[mytext]{\parbox{4cm}{\raggedright Undiversifiable Market Risk (Systematic Risk)}} (axis cs: 7.5,0);
\mycalc{3}{\parbox{4.8cm}{\raggedright Unsystematic Risk eliminated by diversification}}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}