1

I have bar charts that compare computation time of algorithms. The value above each bar represent the time and because they are bigger than they should be, some values are overlapped to graph or another bar.

I tried to rotate the value, but then it looks like the fig.

enter image description here

Is there any way to rotate different way so that some values can be inside the bar and some are not.

Here are my code.

\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}

Torbjørn T.
  • 206,688
Q123
  • 119
  • 3

2 Answers2

2

To answer your specific question, you can add nodes near coords style to each \addplot separately, which means you're free to modify the position and style of each single node separately. The example has some nodes on top of the bars (default style), some rotated, inside the bar, and some rotated, on top of the bar. For the rotation I did basically the same thing you did, except rotate it the other way. (Can change that if you want.)

I also defined a new style for the rotated labels, so I don't have to write the full nodes near coords style each time.

In the code below I also used a groupplot instead of the tabular. It cuts down on the code repetition quite a bit. And I added a second suggestion where I used ymin=0, and placed all the labels in the same way (rotated inside the bars).

\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}

enter image description here

Torbjørn T.
  • 206,688
0
\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}

enter image description here

  • Another thing I didn't point out is that \tikzstyle is considered deprecated, so usually \tikzset{every node/.style={...}} is recommended instead. Or in this case, \begin{tikzpicture}[every node/.style={...}]. However, the real reason I wouldn't use the every node style in this case is that it does change every node. Including the axis label, ticklabels, and legend entries. – Torbjørn T. Nov 16 '21 at 18:48