\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}
答案1
这可能不是最好的解决方案,但对于仅有视图数据点的图来说,将线和标记放在不同的图中并单独调整节点位置就可以完成工作。
\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}
答案2
您可以使用visualization depends on
。添加
visualization depends on={ifthenelse(\coordindex==4,2mm,0) \as \nncshift}
在axis
选项中,在every node near coord
样式中添加
xshift=\nncshift
\coordindex
正如您所料,是坐标的索引,从 0 开始计数。因此,第五个坐标的索引为 4。
如果您想要将第一个和第六个标签稍微左移,您可以嵌套两个ifthenelse
语句,如下所示:
visualization depends on={ifthenelse(\coordindex==4,2mm,ifthenelse(\coordindex==0||\coordindex==5,-2mm,0)) \as \nncshift}
另一条评论:不要使用\addplot plot[..]
,而只使用\addplot [..]
,或\addplot +[..]
。+
表示设置附加到默认设置。plot
关键字用于纯 TikZ,例如\draw plot ...
(代码格式当然是个人喜好,我更喜欢使用更多行,因此重新格式化。)
\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}
答案3
此解决方案为每个节点标签使用特定的锚点。这是通过在数据表中将锚点信息添加为新列来实现的。然后通过添加,anchor=\alignment,
可以visualization depends on=\thisrow{alignment} \as \alignment
将每个标签推到首选位置。alignment = 0 对应西,alignment = 180 对应东,依此类推。我没有中文字体,因此图像中未显示一些图形元素。
这是 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}