如何在轴环境中的每个段上添加图例

如何在轴环境中的每个段上添加图例

我希望在提供的示例所创建的图表的每个部分中都包含文本,例如 n=1、n=2、... n=4。我认为我首先必须更改缩放比例,否则最左侧的部分就没有空间了(从 0 开始轴不是一个选项)。

\documentclass[letter,12pt,twoside]{article}
\usepackage{tikz} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.11} 
\begin{document} 
\begin{tikzpicture} 
\begin{axis}[
    axis lines = left,
    xlabel = $\beta$,
    ylabel = {$E[\pi_B(\beta)]/10^4$}, ] %Below the red parabola is defined \addplot [
    domain=1875/497:4, 
    samples=100, 
    color=red     ] {5*(-(1/3) + 5/x)};

\addplot [
    domain=2500/741:1875/497, 
    samples=100, 
    color=red     ] { 50/3 / x};

\addplot [
    domain=625/196:2500/741, 
    samples=100, 
    color=red   ] { 5*(1/5 + 5/2/x)};

\addplot [
    domain=305/97:625/196, 
    samples=100, 
    color=red ] { 5*(1/3 + 2/x)};

\draw[dashed] (1875/497, 4.4) -- (1875/497,5); \draw[dashed] (2500/741,4.4) -- (2500/741,5); \draw[dashed] (625/196,4.4) -- (625/196,5);

\end{axis} 
\end{tikzpicture}
\end{document}

提前非常感谢您。

答案1

我可能误解了你的问题。你的意思是这样的吗?

在此处输入图片描述

\documentclass[letter,12pt,twoside]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}[
  plotnode/.style={
     midway,
     sloped,
     above,
     font=\footnotesize,
     node contents={ \pgfmathparse{\numplots - \plotnum} $n = \pgfmathprintnumber{\pgfmathresult}$ } % courtesy of Stefan Pinnow, see comment below
     }
]
\begin{axis}[
    axis lines = left,
    xlabel = $\beta$,
    ylabel = {$E[\pi_B(\beta)]/10^4$},
    ]%Below the red parabola is defined

\addplot [
    domain=1875/497:4, 
    samples=100, 
    color=red     ] {5*(-(1/3) + 5/x)}
    node[plotnode];

\addplot [
    domain=2500/741:1875/497, 
    samples=100, 
    color=red     ] { 50/3 / x}
    node[plotnode];

\addplot [
    domain=625/196:2500/741, 
    samples=100, 
    color=red   ] { 5*(1/5 + 5/2/x)}
    node[plotnode];

\addplot [
    domain=305/97:625/196, 
    samples=100, 
    color=red ] { 5*(1/3 + 2/x)}
    node[plotnode];

\draw[dashed] (1875/497, 4.4) -- (1875/497,5);
\draw[dashed] (2500/741, 4.4) -- (2500/741,5);
\draw[dashed] (625/196,4.4) -- (625/196,5);

\end{axis}
\end{tikzpicture}

\end{document}

或者可能是这样:

在此处输入图片描述

\documentclass[letter,12pt,twoside,border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}[
  labelnode/.style={font=\footnotesize, above},
  labelline/.style={stealth-stealth,shorten >=0.1pt, shorten <=0.5pt}
]
\begin{axis}[
    axis lines = left,
    xlabel = $\beta$,
    ylabel = {$E[\pi_B(\beta)]/10^4$},
    width=10cm,
    height=5cm,
    clip mode=individual
    ]

\addplot [
    domain=1875/497:4, 
    samples=100, 
    color=red     ] {5*(-(1/3) + 5/x)} ;

\addplot [
    domain=2500/741:1875/497, 
    samples=100, 
    color=red     ] { 50/3 / x};

\addplot [
    domain=625/196:2500/741, 
    samples=100, 
    color=red   ] { 5*(1/5 + 5/2/x)};

\addplot [
    domain=305/97:625/196, 
    samples=100, 
    color=red ] { 5*(1/3 + 2/x)};

\coordinate (l) at (rel axis cs:0,1.1);

\draw [labelline] (305/97, 0 |- l) -- node[labelnode]{$n=1$} (625/196, 0 |- l);
\draw [labelline] (625/196, 0 |- l) -- node[labelnode]{$n=2$} (2500/741, 0 |- l);
\draw [labelline] (2500/741, 0 |- l) -- node[labelnode]{$n=3$} (1875/497, 0 |- l);
\draw [labelline] (1875/497, 0 |- l) -- node[above]{$n=4$} (4, 0 |- l);

\draw[dashed] (1875/497, 4.4) -- (1875/497,5);
\draw[dashed] (2500/741, 4.4) -- (2500/741,5);
\draw[dashed] (625/196, 4.4) -- (625/196,5);

\end{axis}
\end{tikzpicture}

\end{document}

相关内容