2
\begin{tikzpicture}
\begin{axis}[ylabel near ticks,xlabel near ticks,width=0.95\textwidth,height=8cm,xlabel=(年),ylabel=(亿元),xmin=2010.5,xmax=2017.5,ymin=0,ymax=130,legend style={font=\footnotesize,at={(0.22,0.4)},anchor=south},/pgf/number format/1000 sep=,xtick=data,every node near coord/.append style={font=\scriptsize, /pgf/number format/precision=1,/pgf/number format/fixed zerofill},legend cell align=left,minor tick num=3,ytick={20,40,60,80,100,120}
,every tick label/.append style={font=\footnotesize},every axis x label/.style={at={(0.97,-0.05)},anchor=west,font=\footnotesize},mark size={3},
every axis y label/.style={at={(-0.05,1.05)},anchor=north,font=\footnotesize}]

    \addplot plot[thin,smooth,mark=10-pointed star,nodes near coords,every node near coord/.append style={swap,font=\footnotesize,anchor=-50,xshift=2mm
}] coordinates {(2011,61)(2012,97.9)(2013,87.5)(2014,91.7)(2015,45.2)(2016,38.6)(2017,120)}
    node[below,font=\footnotesize](dt)at(axis cs:2015.7,80){1-3月投资完成曲线};
    \draw[->,>=stealth',shorten >=1pt,ultra thick,blue]
(dt)edge(axis cs:2014.6,60);
\end{axis}
\end{tikzpicture}

enter image description here

  • 3
    Welcome to TeX.SX! Please make your code compilable, completing it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – CarLaTeX Aug 4 '17 at 5:55
2

This may not be the best solution but for a plot with only a view data points, putting the line and markers in different plots and adjusting the node positions individually does the job.

\documentclass{standalone}
\usepackage[UTF8]{ctex}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}

\begin{axis}[
  ylabel near ticks,
  xlabel near ticks,
  width=0.95\textwidth,
  height=8cm,
  xlabel=(年),
  ylabel=(亿元),
  xmin=2010.5,
  xmax=2017.5,
  ymin=0,
  ymax=130,
  legend style={
    font=\footnotesize,
    at={(0.22,0.4)},
    anchor=south},
  /pgf/number format/1000 sep=,
  xtick=data,
  every node near coord/.append style={
    font=\scriptsize, 
    /pgf/number format/precision=1,
    /pgf/number format/fixed zerofill},
  legend cell align=left,
  minor tick num=3,
  ytick={20,40,60,80,100,120},
  every tick label/.append style={font=\footnotesize},
  every axis x label/.style={
    at={(0.97,-0.05)},
    anchor=west,
    font=\footnotesize},
  mark size={3},
  every axis y label/.style={
    at={(-0.05,1.05)},
    anchor=north,
    font=\footnotesize}
]

\addplot plot[
  thin,
  smooth,
  no markers,
] 
coordinates {
  (2011,61)
  (2012,97.9)
  (2013,87.5)
  (2014,91.7)
  (2015,45.2)
  (2016,38.6)
  (2017,120)
};

\addplot [
  only marks,
  blue,
  mark=10-pointed star,
  nodes near coords,
  every node near coord/.append style={
    swap,
    font=\footnotesize,
    anchor=-90}
]
coordinates {
   (2012,97.9)
   (2013,87.5)
   (2014,91.7)
   (2017,120)
};

\addplot [
  only marks,
  blue,
  mark=10-pointed star,
  nodes near coords,
  every node near coord/.append style={
    swap,
    font=\footnotesize,
    anchor=-180}
]
coordinates {
   (2011,61)
   (2015,45.2)
   (2016,38.6)
}
node [
    below, 
    font=\footnotesize
] 
(dt) at (axis cs:2015.5,80) 
{1-3月投资完成曲线};

\draw [
    ->, 
    >=stealth', 
    shorten >=1pt, 
    ultra thick, 
    blue
] 
(dt) edge (axis cs:2014.64,60);

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

Output

2

You can make use of visualization depends on. Add

visualization depends on={ifthenelse(\coordindex==4,2mm,0) \as \nncshift}

to the axis options, and in the every node near coord style, add

xshift=\nncshift

\coordindex is, as you may expect, an index for the coordinates, which starts counting at 0. Hence, the fifth coordinate has index 4.

If you'd like to shift the first and sixth label left a bit, you can nest two ifthenelse statements, as such:

visualization depends on={ifthenelse(\coordindex==4,2mm,ifthenelse(\coordindex==0||\coordindex==5,-2mm,0)) \as \nncshift}

One other comment: Don't use \addplot plot[..], but just \addplot [..], or \addplot +[..]. The + indicates that the settings are appended to the default settings. The plot keyword is for use with pure TikZ, e.g. \draw plot ...

(Code formatting is of course a personal preference, I'd prefer using more lines, hence the reformatting.)

\documentclass[12pt,border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ylabel near ticks,
  xlabel near ticks,
  width=0.95\textwidth,
  height=8cm,
  xlabel=(年),
  ylabel=(亿元),
  xmin=2010.5,
  xmax=2017.5,
  ymin=0,
  ymax=130,
  legend style={
    font=\footnotesize,
    at={(0.22,0.4)},
    anchor=south
  },
  /pgf/number format/1000 sep=,
  xtick=data,
  every node near coord/.append style={
    font=\scriptsize,
    /pgf/number format/precision=1,
    /pgf/number format/fixed zerofill,
    xshift=\nncshift % added
  },
  legend cell align=left,
  minor tick num=3,
  ytick={20,40,60,80,100,120},
  every tick label/.append style={
    font=\footnotesize
  },
  every axis x label/.style={
    at={(0.97,-0.05)},
    anchor=west,
    font=\footnotesize
  },
  mark size={3},
  every axis y label/.style={
    at={(-0.05,1.05)},
    anchor=north,
    font=\footnotesize
  },
  % the following line is added
  visualization depends on={ifthenelse(\coordindex==4,2mm,0) \as \nncshift}
]

  \addplot [
    thin,
    smooth,
    mark=10-pointed star,
    nodes near coords,
] coordinates {(2011,61)(2012,97.9)(2013,87.5)(2014,91.7)(2015,45.2)(2016,38.6)(2017,120)};

  \node[below,font=\footnotesize](dt) at(axis cs:2015.7,80){1-3月投资完成曲线}; 
  \draw[->,>=stealth,shorten >=1pt,ultra thick,blue] (dt) edge(axis cs:2014.6,60);
\end{axis}
\end{tikzpicture}
\end{document}
2

This solution uses specific anchor for each node label. This is done by adding the anchor information as a new column in the data table. Then by adding anchor=\alignment, and visualization depends on=\thisrow{alignment} \as \alignment each label can be nudged to a preferred position. alignment = 0 corresponds to west, alignment = 180 corresponds to east and so on. I don't have the Chinese fonts so a few graph elements are not shown in the image.

This is the result: enter image description here

This is the MWE:

\documentclass[border=5pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
scatter,
ylabel near ticks,
xlabel near ticks,
width=0.95\textwidth,
height=8cm,
xlabel=(年),
ylabel=(亿元),
xmin=2010.5,
xmax=2017.5,
ymin=0,
ymax=130,
legend style={
    font=\footnotesize,
    at={(0.22,0.4)},
    anchor=south},
/pgf/number format/1000 sep=,
xtick=data,
every node near coord/.append style={
    font=\scriptsize,
    /pgf/number format/precision=1,
    /pgf/number format/fixed zerofill},
legend cell align=left,
minor tick num=3,
ytick={20,40,60,80,100,120},
every tick label/.append style={
    font=\footnotesize},
every axis x label/.style={
    at={(0.97,-0.05)},
    anchor=west,
    font=\footnotesize},
mark size={3},
nodes near coords,
every node near coord/.append style={
    swap,
    font=\footnotesize,
    anchor=\alignment, % <- added this
    },
every axis y label/.style={
    at={(-0.05,1.05)},
    anchor=north,
    font=\footnotesize},
visualization depends on=\thisrow{alignment} \as \alignment % <- added this
]

\addplot [thin,
    smooth,
    mark=10-pointed star,
    ] table {
     x y alignment % <- added alignment settings
    2011 61 90
    2012 97.9 270
    2013 87.5 90
    2014 91.7 225
    2015 45.2 225
    2016 38.6 180
    2017 120 0
    };
\node[below,font=\footnotesize](dt)at(axis cs:2015.7,80){1-3月投资完成曲线};
\draw[->,>=stealth',shorten >=1pt,ultra thick,blue]
(dt)edge(axis cs:2014.6,60);
\end{axis}
\end{tikzpicture}

\end{document}
  • Thanks, it is a good idea, and your code is simple and nice. – J.Luo Aug 9 '17 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.