latex 条形图值重叠。如果我旋转,则看起来很奇怪

latex 条形图值重叠。如果我旋转,则看起来很奇怪

我有比较算法计算时间的条形图。每个条形上方的值代表时间,由于它们比实际值大,因此一些值与图形或另一个条形重叠。

我尝试旋转该值,但它看起来就像图中这样。

在此处输入图片描述

有没有办法以不同的方式旋转,以便某些值可以在栏内,而某些值不在。

这是我的代码。

\documentclass{article}
\usepackage{geometry}

\usepackage{pgfplots} % package used to implement the plot  
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{width=8.5cm, compat=1.6}  

\begin{document}

\begin{figure}
\begin{tikzpicture}  
\hspace*{-2.1cm}%
\begin{tabular}{c@{\hspace{2.5cm}}c@{\hspace{2.5cm}}c@{\hspace{2.5cm}}c@{\hspace{2.5cm}}c}
\begin{axis}  
[  
    width=3cm,
    height=5.2cm,
    ybar, 
    enlarge y limits=0.15, 
    enlarge x limits=0.3,
    legend style={at={(0.4,-0.25)}, 
    anchor=north,legend columns=-1},
    ylabel= Execution Time (ms),   
    xlabel= number of points,
    symbolic x coords={10K},  
    xtick=data,  
    nodes near coords,  
    nodes near coords align={vertical},  
    nodes near coords style={anchor=east,rotate=-90,inner xsep=1pt},
    ]  
\addplot coordinates {(10K, 0.369)  }; 
\addplot coordinates {(10K, 0.325)};  
\addplot coordinates {(10K, 0.302) };  
\legend{}  
  
\end{axis}   & 
\begin{axis}  
[  
    width=3cm,
    height=5.2cm,
    ybar, 
    enlarge y limits=0.15, 
    enlarge x limits=0.3,
    legend style={at={(0.4,-0.25)}, 
    anchor=north,legend columns=-1},
    ylabel= ,   
    symbolic x coords={100K},  
    xtick=data,  
    nodes near coords,  
    nodes near coords align={vertical},  
    ]  
\addplot coordinates {(100K, 3.392)  }; 
\addplot coordinates {(100K, 3.249)};  
\addplot coordinates {(100K, 2.861) };  
\legend{}  
  
\end{axis}  &
\begin{axis}  
[  
    width=3cm,
    height=5.2cm,
    ybar, 
    enlarge y limits=0.15, 
    enlarge x limits=0.3,
    legend style={at={(0.4,-0.25)}, 
    anchor=north,legend columns=-1},
    ylabel= ,   
    symbolic x coords={1M},  
    xtick=data,  
    nodes near coords,  
    nodes near coords align={vertical},  
    ]  
\addplot coordinates {(1M, 34.729)  }; 
\addplot coordinates {(1M, 33.818)};  
\addplot coordinates {(1M, 29.640) };  
\legend{Algo1, Algo2, Ours}  
  
\end{axis}  &
\begin{axis}  
[  
    width=3cm,
    height=5.2cm,
    ybar, 
    enlarge y limits=0.15, 
    enlarge x limits=0.3,
    legend style={at={(0.4,-0.25)}, 
    anchor=north,legend columns=-1},
    ylabel= ,   
    symbolic x coords={10M},  
    xtick=data,  
    nodes near coords,  
    nodes near coords align={vertical},  
    ]  
\addplot coordinates {(10M, 387.236)  }; 
\addplot coordinates {(10M, 332.314)};  
\addplot coordinates {(10M, 278.019) };  
\legend{}  
  
\end{axis}  &
\begin{axis}  
[  
    width=3cm,
    height=5.2cm,
    ybar, 
    enlarge y limits=0.15, 
    enlarge x limits=0.3,
    legend style={at={(0.4,-0.25)}, 
    anchor=north,legend columns=-1},
    ylabel= ,   
    symbolic x coords={100M},  
    xtick=data,  
    nodes near coords,  
    nodes near coords align={vertical},  
    ]  
\addplot coordinates {(100M, 4195.304)  }; 
\addplot coordinates {(100M, 3188.144)};  
\addplot coordinates {(100M, 2683.925) };  
\legend{}  
  
\end{axis}  
\end{tabular}


\end{tikzpicture}  
\caption{\label{fig:comparison1}Average computation time in milliseconds for points in a square-boundary.}
\end{figure}
\end{document}

答案1

要回答您的具体问题,您可以分别添加nodes near coords style每个节点\addplot,这意味着您可以自由地分别修改每个单个节点的位置和样式。该示例有一些节点位于栏顶部(默认样式),一些节点旋转,位于栏内,一些节点旋转,位于栏顶部。对于旋转,我所做的基本与您相同,只是以另一种方式旋转。(如果您愿意,可以更改。)

我还为旋转的标签定义了一种新样式,这样我就不必nodes near coords style每次都写完整的内容。

在下面的代码中,我还使用了groupplot而不是tabular。这大大减少了代码重复。我添加了第二个建议,我使用了ymin=0,并以相同的方式放置所有标签(在条形内旋转)。

\documentclass{article}
\usepackage{geometry}

\usepackage{pgfplots} % package used to implement the plot  
\usepgfplotslibrary{groupplots}
\pgfplotsset{width=8.5cm, compat=1.6}  

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}  
\begin{groupplot}  
[  
    group style={
       group size=5 by 1,
       ylabels at=edge left,
       horizontal sep=1cm,
    },
    width=3cm,
    height=5.2cm,
    ybar,
    enlarge y limits=0.15, 
    enlarge x limits=0.3,
    legend style={at={(0.5,-0.5)}, 
    anchor=north,legend columns=-1},
    ylabel= Execution Time (ms),   
    xlabel={number\\of points},
    xlabel style={align=center},
    xtick=data,  
    nodes near coords,  
    nodes near coords align={vertical},  
    nodes near coords style={font=\scriptsize},
    insideBar/.style={ % define a new style for placing node inside bar
      nodes near coords style={
        anchor=east,
        rotate=90,
        font=\scriptsize,
        left,
        #1}
    }
   ]  
    
\nextgroupplot[symbolic x coords={10K}]

\addplot coordinates {(10K, 0.369)  }; 
\addplot coordinates {(10K, 0.325)};  
\addplot coordinates {(10K, 0.302) };  
  
\nextgroupplot[symbolic x coords={100K}]
\addplot coordinates {(100K, 3.392)  }; 
\addplot coordinates {(100K, 3.249)};  
\addplot coordinates {(100K, 2.861) };  

\nextgroupplot[symbolic x coords={1M}]
\addplot +[insideBar] coordinates {(1M, 34.729)}; % <-- the + indicates the style is appended to the default style, instead of replacing it
\addplot +[insideBar] coordinates {(1M, 33.818)};  
\addplot +[insideBar={right}] coordinates {(1M, 29.640) };  % <-- added right to place node above bar instead of inside
\legend{Algo1, Algo2, Ours}  

\nextgroupplot[symbolic x coords={10M}]
\addplot +[insideBar] coordinates {(10M, 387.236)  }; 
\addplot +[insideBar] coordinates {(10M, 332.314)};  
\addplot +[insideBar={right}] coordinates {(10M, 278.019) };  

\nextgroupplot[symbolic x coords={100M}]
\addplot +[insideBar] coordinates {(100M, 4195.304)  }; 
\addplot +[insideBar] coordinates {(100M, 3188.144)};  
\addplot +[insideBar={right}] coordinates {(100M, 2683.925) };  
  
\end{groupplot}


\end{tikzpicture}  
\caption{\label{fig:comparison1}Average computation time in milliseconds for points in a square-boundary.}
\end{figure}


\begin{figure}
\centering
\begin{tikzpicture}  
\begin{groupplot}  
[  
    group style={
       group size=5 by 1,
       ylabels at=edge left,
       horizontal sep=1cm,
    },
    width=3cm,
    height=5.2cm,
    ybar,
    ymin=0,
    enlarge y limits={upper=0.15}, % <-- modified
    enlarge x limits=0.3,
    legend style={at={(0.5,-0.5)}, 
    anchor=north,legend columns=-1},
    ylabel= Execution Time (ms),   
    xlabel={number\\of points},
    xlabel style={align=center},
    xtick=data,  
    nodes near coords,  
    nodes near coords align={vertical},  
    nodes near coords style={font=\scriptsize},
    nodes near coords style={
        anchor=east,
        rotate=90,
        font=\scriptsize,
        #1
    }
   ]  
    
\nextgroupplot[symbolic x coords={10K}]

\addplot coordinates {(10K, 0.369)  }; 
\addplot coordinates {(10K, 0.325)};  
\addplot coordinates {(10K, 0.302) };  
  
\nextgroupplot[symbolic x coords={100K}]
\addplot coordinates {(100K, 3.392)  }; 
\addplot coordinates {(100K, 3.249)};  
\addplot coordinates {(100K, 2.861) };  

\nextgroupplot[symbolic x coords={1M}]
\addplot coordinates {(1M, 34.729)  }; 
\addplot coordinates {(1M, 33.818)};  
\addplot coordinates {(1M, 29.640) };  
\legend{Algo1, Algo2, Ours}  

\nextgroupplot[symbolic x coords={10M}]
\addplot coordinates {(10M, 387.236)  }; 
\addplot coordinates {(10M, 332.314)};  
\addplot coordinates {(10M, 278.019) };  

\nextgroupplot[symbolic x coords={100M}]
\addplot coordinates {(100M, 4195.304)  }; 
\addplot coordinates {(100M, 3188.144)};  
\addplot coordinates {(100M, 2683.925) };  
  
\end{groupplot}


\end{tikzpicture}  
\caption{\label{fig:comparison1}Average computation time in milliseconds for points in a square-boundary.}
\end{figure}
\end{document}

在此处输入图片描述

答案2

\usepackage{geometry}

\usepackage{pgfplots} % package used to implement the plot  
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{width=8.5cm, compat=1.6}  

\begin{document}
    
    \begin{figure}
        \begin{tikzpicture}  
            \tikzstyle{every node}=[font=\tiny] %<<--- line added
            \hspace*{0.8cm} %<<--- line modified for centering
            \begin{tabular}{c@{\hspace{2.5cm}}c@{\hspace{2.5cm}}c@{\hspace{2.5cm}}c@{\hspace{2.5cm}}c}
                \begin{axis}  
                    [  
                    width=3cm,
                    height=5.2cm,
                    ybar, 
                    enlarge y limits=0.15, 
                    enlarge x limits=0.3,
                    legend style={at={(0.4,-0.25)}, 
                        anchor=north,legend columns=-1},
                    ylabel= Execution Time (ms),   
                    xlabel= number of points,
                    symbolic x coords={10K},  
                    xtick=data,  
                    nodes near coords,  
                    nodes near coords align={vertical},  
                    nodes near coords style={anchor=east,rotate=-90,inner xsep=1pt}
                    ]  
                    \addplot coordinates {(10K, 0.369)  }; 
                    \addplot coordinates {(10K, 0.325)};  
                    \addplot coordinates {(10K, 0.302) };  
                    \legend{}  
                    
                \end{axis}   & 
                \begin{axis}  
                    [  
                    width=3cm,
                    height=5.2cm,
                    ybar, 
                    enlarge y limits=0.15, 
                    enlarge x limits=0.3,
                    legend style={at={(0.4,-0.25)}, 
                        anchor=north,legend columns=-1},
                    ylabel= ,   
                    symbolic x coords={100K},  
                    xtick=data,  
                    nodes near coords,  
                    nodes near coords align={vertical},  
                    ]  
                    \addplot coordinates {(100K, 3.392)  }; 
                    \addplot coordinates {(100K, 3.249)};  
                    \addplot coordinates {(100K, 2.861) };  
                    \legend{}  
                    
                \end{axis}  &
                \begin{axis}  
                    [  
                    width=3cm,
                    height=5.2cm,
                    ybar, 
                    enlarge y limits=0.15, 
                    enlarge x limits=0.3,
                    legend style={at={(0.4,-0.25)}, 
                        anchor=north,legend columns=-1},
                    ylabel= ,   
                    symbolic x coords={1M},  
                    xtick=data,  
                    nodes near coords,  
                    nodes near coords align={vertical},  
                    ]  
                    \addplot coordinates {(1M, 34.729)  }; 
                    \addplot coordinates {(1M, 33.818)};  
                    \addplot coordinates {(1M, 29.640) };  
                    \legend{Algo1, Algo2, Ours}  
                    
                \end{axis}  &
                \begin{axis}  
                    [  
                    width=3cm,
                    height=5.2cm,
                    ybar, 
                    enlarge y limits=0.15, 
                    enlarge x limits=0.3,
                    legend style={at={(0.4,-0.25)}, 
                        anchor=north,legend columns=-1},
                    ylabel= ,   
                    symbolic x coords={10M},  
                    xtick=data,  
                    nodes near coords,  
                    nodes near coords align={vertical},  
                    ]  
                    \addplot coordinates {(10M, 387.236)  }; 
                    \addplot coordinates {(10M, 332.314)};  
                    \addplot coordinates {(10M, 278.019) };  
                    \legend{}  
                    
                \end{axis}  &
                \begin{axis}  
                    [  
                    width=3cm,
                    height=5.2cm,
                    ybar, 
                    enlarge y limits=0.15, 
                    enlarge x limits=0.3,
                    legend style={at={(0.4,-0.25)}, 
                        anchor=north,legend columns=-1},
                    ylabel= ,   
                    symbolic x coords={100M},  
                    xtick=data,  
                    nodes near coords,  
                    nodes near coords align={vertical},  
                    ]  
                    \addplot coordinates {(100M, 4195.304)  }; 
                    \addplot coordinates {(100M, 3188.144)};  
                    \addplot coordinates {(100M, 2683.925) };  
                    \legend{}  
                    
                \end{axis}  
            \end{tabular}
            
            
        \end{tikzpicture}  
        \caption{\label{fig:comparison1}Average computation time in milliseconds for points in a square-boundary.}
    \end{figure}
\end{document}

在此处输入图片描述

相关内容