我创建了一条包含两个图的图表线并使用启用了标记标签nodes near coords
,但我得到了重叠的标签,如下图所示:
我该如何解决这个问题?有没有办法让标签不重叠?
\begin{figure}[H]
\centering
\begin{tikzpicture}[scale=0.9]
\begin{axis}[legend pos=south east, scale = 1, xlabel=\textbf{data},ylabel=\textbf{values}, xticklabels={
n10-1,
n10-2,
n30-1,
n30-2,
n50-1,
n50-2,
n70-1,
n70-2,
n100-1,
n100-2,
n150-1,
n150-2,
n200-1,
n200-2}, xtick={1,...,14}, x tick label style={rotate=45,anchor=east},legend style={ at={($(0.5, 1.1)$)},anchor=center, legend columns=2},
height=7cm,
width=\textwidth,
enlarge x limits=0.08,
ymajorgrids = true,
grid style=dashed,
nodes near coords
],
\addplot [blue, mark size= 2pt, mark = square*, mark options=solid] coordinates {
(1,6.44)
(2,4.89)
(3,42.12)
(4,41.66)
(5,151.19)
(6,114.45)
(7,99.11)
(8,137.8)
(9,103.42)
(10,112.87)
(11,214.07)
(12,288.96)
(13,271.66)
(14,418.66)
};
\addlegendentry{A};
\addplot [black, mark size= 2pt, mark = square*, mark options=solid] coordinates {
(1,20.73)
(2,19.31)
(3,91.22)
(4,77.76)
(5,170.58)
(6,136.79)
(7,143.81)
(8,213.29)
(9,122.17)
(10,145.95)
(11,225.66)
(12,293.39)
(13,319.23)
(14,439.86)
};
\addlegendentry{B};
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
您可以使节点变小和/或将它们移动到上方/下方,如下所示:
\documentclass[tikz,border=10mm]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=0.9]
\begin{axis}[legend pos=south east, scale = 1, xlabel=\textbf{data},ylabel=\textbf{values}, xticklabels={
n10-1,
n10-2,
n30-1,
n30-2,
n50-1,
n50-2,
n70-1,
n70-2,
n100-1,
n100-2,
n150-1,
n150-2,
n200-1,
n200-2}, xtick={1,...,14}, x tick label style={rotate=45,anchor=east},legend style={ at={($(0.5, 1.1)$)},anchor=center, legend columns=2},
height=7cm,
width=\textwidth,
enlarge x limits=0.08,
ymajorgrids = true,
grid style=dashed,
nodes near coords,
nodes near coords style={font=\tiny}
],
\addplot [blue, mark size= 2pt, mark = square*, mark options=solid, nodes near coords align={below}] coordinates {
(1,6.44)
(2,4.89)
(3,42.12)
(4,41.66)
(5,151.19)
(6,114.45)
(7,99.11)
(8,137.8)
(9,103.42)
(10,112.87)
(11,214.07)
(12,288.96)
(13,271.66)
(14,418.66)
};
\addlegendentry{A};
\addplot [black, mark size= 2pt, mark = square*, mark options=solid,nodes near coords align={above}] coordinates {
(1,20.73)
(2,19.31)
(3,91.22)
(4,77.76)
(5,170.58)
(6,136.79)
(7,143.81)
(8,213.29)
(9,122.17)
(10,145.95)
(11,225.66)
(12,293.39)
(13,319.23)
(14,439.86)
};
\addlegendentry{B};
\end{axis}
\end{tikzpicture}
\end{document}