改善 pgfplots 中的标签放置

改善 pgfplots 中的标签放置

这是我第一次使用绘图pgfplots,我对结果很满意。

在此处输入图片描述

但是,我相信我的代码可以改进。特别是,我觉得一定存在一种比“绘制”白线或点以将其用作标签参考更好的放置标签的方法。另外,我宁愿将标签放在直线的末端附近,但我还没有设法将它们放在那里。最后,我认为将轴标签放在端点而不是中间的代码并不是最简单的。

我的代码如下

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{pgfplots}

\usetikzlibrary{spy}

\pgfplotsset{compat = newest}

\DeclareMathOperator{\conv}{conv}

\begin{document}
\begin{tikzpicture}[scale=1,spy using outlines={circle, magnification=4, size=4cm, connect spies}]
\begin{axis}[
     ticks=none, 
     axis x line=bottom,
     axis y line=left,
     xlabel={$q_1$},
     ylabel={$q_2$},

% I just want labels to be placed at the end on the axis. Can a simpler instruction do?

     every axis x label/.style={at={(ticklabel* cs:1.05)},anchor=west},
     every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south},

     xmin=0,xmax=30,
     ymin=0,ymax=18]

% This merely places the label conv C(q) Isn't there a better way to do it?

     \addplot[only marks, white] table{
       17.3 7.875 
       } node[above,right]{\color{black}$\conv C(\hat q)$};

     \draw[fill=gray!30!white] (23.125,4.625) -- (4.625,13.875) -- (6.9375,6.9375) -- cycle;

% I'd like this label closer to the end of the line
     \addplot[
       domain = 0:30,
       very thick
       ] {3*x)} node[very near start,above,left]{$\mathbf x^{i^*}$};

% This one too
     \addplot[
       domain = 0:30,
       thick
       ] {x/5)} node[midway,below]{$\mathbf x^{r}$};

     \addplot[
       domain = 0:30,
       very thick
       ] {x} node[midway,below,right]{$\mathbf x^{i^*+1}$};
     \addplot[
       domain = 0:30,
       very thin,
       dotted
       ] {1.5*x};
     \addplot[only marks, red] table{
       7 10.5
       } node[above,right]{\color{black}$\mathbf{k}^e$};
     \addplot[only marks, green!50!blue] table{
       6.1667 9.25
       } node[above,left]{\color{black}$\mathbf{\bar x}$};
     \spy [black, size=5cm] on (1.55,3.15) in node [right] at (6,5);
  \end{axis}
\end{tikzpicture}
\end{document}

相关内容