我想要绘制一个图表,如下图所示:
我得到的只有这个:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlargelimits=false,
xlabel=$p$,ylabel=$\pi_1$,
domain=0:1,xmin=0,xmax=1,
ymin=0,ymax=4.2,
no marks,clip=false]
\addplot[dashed] { -x+3} node[above,sloped,pos=0.2] {$\pi_1((p,1-p),T)$};
\addplot[dashed] {3*x+1} node[above,sloped,pos=0.7] {$\pi_1((p,1-p),H)$};
\addplot[red] {ifthenelse(x>0.5,-x+3,3*x+1)};
\draw[dotted,red] (axis cs:0,5/2) node[left] {$\underline{v} = \frac{5}{2}$}
-| (axis cs:0.5,0) node[below] {$\hat{p} = \frac{1}{2}$};
\end{axis}
\end{tikzpicture}
还有一些事情没有做到,无法使它与我希望的图表相似。例如,我没有右轴的标签。而且两个轴的末端也没有箭头。此外,标签不在我想要的位置。而且我无法将p=0
resp.p=1
放在它应该在的位置。我真的希望让它看起来相似,但我真的不知道该怎么做。这是我在 LaTeX 中的第一步。我希望有人能帮助我解决这些问题。提前谢谢大家!!!(抱歉我的英语不好)
答案1
附录:
最后,我要用 来解开一个谜题xtick
,因此,extra x ticks
在第一个(现已过时并被删除)解决方案中使用的 不再是必要的,并且代码现在更加简单:
\documentclass[border=3mm,prewiev]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=8cm, compat=1.13}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{% <-- common presets for both axes
ylabel=$\pi_1$,
domain=0:1,xmin=0,xmax=1,
ymin=0,ymax=4.3,
enlargelimits=false,
clip=false
}
\begin{axis}[
axis y line=left, % <-- left y axis
ylabel style={at={(0,1)},rotate=-90,anchor=east},% <--
axis x line=bottom, % <--
x axis line style={-}, % <--
xlabel=$p$,
xtick={0, 0.25, 0.75, 1}, % <--
xticklabels={$p=0$, $\frac{1}{4}$, $\frac{3}{4}$, $p=1$},% <--
]
\addplot[dashed] { -x+3} node[above,sloped,pos=0.2] {$\pi_1((p,1-p),T)$};
\addplot[dashed] {3*x+1} node[above,sloped,pos=0.7] {$\pi_1((p,1-p),H)$};
\addplot[red] {ifthenelse(x>0.5,-x+3,3*x+1)};
\draw[dotted,red] (0,5/2) node[left] {$\underline{v} = \frac{5}{2}$}
-| (0.5,0) node[below] {$\hat{p} = \frac{1}{2}$};
\end{axis}
%
\begin{axis}[
axis y line=right, % <-- right y axis
ylabel style={at={(1,1)},rotate=-90,anchor=west},% <--
axis x line=none, % <--
]
\end{axis}
\end{tikzpicture}
\end{document}
在上面的代码中,与所指定的 MWE 相比,添加了一些东西% <--
。结果是:
答案2
有时,当你想在图表中添加很多灵活性和额外的细节时,值得花点功夫“从头”绘制图表元帖子。这里我把它包起来了,luamplib
所以你需要用它来处理它lualatex
(或者让它适应普通的 MP)。
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
% units - horizontal and vertical
u = 10cm;
v = 1.2cm;
path xx, yy, yyy;
xx = origin -- right scaled u;
yy = origin -- up scaled 4.4v;
yyy = yy shifted (1u,0);
z1 = (0,1v); z2 = (1u,2v);
z3 = (0,3v); z4 = (1u,4v);
z5 = (1/2u,5/2v);
draw z1 -- z5 -- z2 withcolor red;
draw (0,y5) -- z5 -- (x5,0) dashed withdots scaled 1/2 withcolor red;
path line[];
line1 = z1--z4;
line2 = z3--z2;
draw line1 dashed evenly;
draw line2 dashed evenly;
draw textext("$\pi_1\bigl((p, 1-p),H\bigr)$")
rotated angle direction 5/8 of line1
shifted point 5/8 of line1
shifted 6 up;
draw textext("$\pi_1\bigl((p, 1-p),T\bigr)$")
rotated angle direction 1/8 of line2
shifted point 1/8 of line2
shifted 6 up;
for i=1 upto 4:
draw (origin -- 3 left) shifted (0,i*v);
draw (origin -- 3 right) shifted (u,i*v);
label.lft(decimal i, (0-3,i*v));
label.rt (decimal i, (u+3,i*v));
endfor
for i=1 upto 3:
draw (origin -- 3 down) shifted (i/4*u,0);
endfor
label.bot("$p=0$", ( 0,-5));
label.bot("$p=1$", ( 1u,-5));
label.bot("$\frac14$", (1/4u,-3));
label.bot("$\frac34$", (3/4u,-3));
label.bot("$\hat{p}=\frac12$",(1/2u,-3)) withcolor red;
label.lft("$v=\frac52$", (-3,5/2v)) withcolor red;
draw xx;
drawarrow yy; label.top("$\pi_1$", point 1 of yy);
drawarrow yyy;label.top("$\pi_1$", point 1 of yyy);
endfig;
\end{mplibcode}
\end{document}