1

I want hop1 to hop 6 tiles on every group of bars but i am unable to create it. Similarly I want to add similar title on trendlines drawn for every hop. Your help in this regard would be highly appreciated.

Thank you.

Below is the compileable code attached

\RequirePackage{filecontents}

\begin{filecontents*}{hop1}
1pkt/5sec 492
1pkt/10sec 483
1pkt/20sec 479
1pkt/30sec 420
1pkt/40sec 493
1pkt/50sec 500
\end{filecontents*}

\begin{filecontents*}{hop2}
1pkt/5sec 457
1pkt/10sec 409
1pkt/20sec 440
1pkt/30sec 50
1pkt/40sec 490
1pkt/50sec 498
\end{filecontents*}

\begin{filecontents*}{hop3}
1pkt/5sec 407
1pkt/10sec 438
1pkt/20sec 399
1pkt/30sec 322
1pkt/40sec 412
1pkt/50sec 497
\end{filecontents*}

\begin{filecontents*}{hop4}
1pkt/5sec 63
1pkt/10sec 156
1pkt/20sec 370
1pkt/30sec 331
1pkt/40sec 357
1pkt/50sec 496
\end{filecontents*}

\begin{filecontents*}{hop5}
1pkt/5sec 90
1pkt/10sec 206
1pkt/20sec 368
1pkt/30sec 323
1pkt/40sec 379
1pkt/50sec 490
\end{filecontents*}

\begin{filecontents*}{hop6}
1pkt/5sec 56
1pkt/10sec 240
1pkt/20sec 0
1pkt/30sec 324
1pkt/40sec 376
1pkt/50sec 484
\end{filecontents*}



\documentclass[conference]{IEEEtran}
\ifCLASSINFOpdf
\else
\fi
\usepackage[pdftex]{graphics}
\usepackage{pgfplots,pgfplotstable}
\usetikzlibrary{patterns}
\usepgfplotslibrary{dateplot}
\usepackage{cite}
\usepackage{url}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=1.7}

\hyphenation{op-tical net-works semi-conduc-tor}

\pgfplotstableread{hop1}\a
\pgfplotstableset{create on use/x/.style={create col/expr={\pgfplotstablerow}}}
\pgfplotstableread{hop2}\b
\pgfplotstableset{create on use/x/.style={create col/expr={\pgfplotstablerow}}}
\pgfplotstableread{hop3}\c
\pgfplotstableset{create on use/x/.style={create col/expr={\pgfplotstablerow}}}
\pgfplotstableread{hop4}\d
\pgfplotstableset{create on use/x/.style={create col/expr={\pgfplotstablerow}}}
\pgfplotstableread{hop5}\e
\pgfplotstableset{create on use/x/.style={create col/expr={\pgfplotstablerow}}}
\pgfplotstableread{hop6}\f
\pgfplotstableset{create on use/xnum/.style={create col/expr={\pgfplotstablerow}}}
\begin{document} 
\begin{figure*}[t]
\begin{tikzpicture}
\begin{axis}[
height=6cm, width=13.5cm,
%x tick label style={rotate=45},
ybar,
bar width=0.2cm,
xlabel={Pkt Generation Rate},
ylabel={Pkt Delivery Ratio},
ymin=0, ymax=500,
symbolic x coords={1pkt/5sec,1pkt/10sec,1pkt/20sec,1pkt/30sec,1pkt/40sec,1pkt/50sec},
xtick=data,
ytick={0,100,200,300,400,500,600},
legend style={font=\small},
legend pos=outer north east,
ymajorgrids=true,
legend columns=2,
%legend style={at={(0.5,0.5)}},
%grid style=dashed,
]
 \addplot[red!20!black,fill=black!80!white] table {\a};
  \addlegendentry{Hop-1};
   \addplot+[smooth,black!80,line width=1pt]table [y={create col/linear regression={x=x}}] {\a};
  \addlegendentry{Trendline hop-1};
\addplot[red!20!black,fill=blue!80!white] table {\b};
  \addlegendentry{Hop-2};
  \addplot+[smooth,blue!80,line width=1pt] table [y={create col/linear regression={x=x}}] {\b};
  \addlegendentry{Trendline hop-2};
\addplot[red!20!black,fill=red!80!white] table {\c};
  \addlegendentry{Hop-3};
   \addplot[smooth,red!80,line width=1pt] table [y={create col/linear regression={x=x}}] {\c};
  \addlegendentry{Trendline hop-3};
\addplot[red!20!black,fill=orange!80!white] table {\d};
  \addlegendentry{Hop-4};
  \addplot[smooth,orange!80,line width=1pt] table [y={create col/linear regression={x=x}}] {\d};
  \addlegendentry{Trendline hop-4};
\addplot[red!20!black,fill=green!80!white] table {\e};
  \addlegendentry{Hop-5};
   \addplot[smooth,green!80,line width=1pt] table [y={create col/linear regression={x=x}}] {\e};
  \addlegendentry{Trendline hop-5};
\addplot[red!20!black,fill=cyan!80!white] table {\f};
  \addlegendentry{Hop-6};
  \addplot[smooth,cyan!100,line width=1pt] table [y={create col/linear regression={x=x}}] {\f};
  \addlegendentry{Trendline hop-6};
\end{axis}
\end{tikzpicture}
\end{figure*}
\end{document} 

Here is the output

enter image description here

I want 1 written on top of every black bar, 2 on every blue bar and so on. Example is attached

enter image description here

2
  • Can you draw a sketch in Paint or similar of what you're after? It's a bit unclear to me exactly where you want these titles placed. May 18, 2018 at 8:12
  • You may find it in edited version. Thank you May 18, 2018 at 10:33

1 Answer 1

1

I've drastically rearranged the code to make it shorter. I've removed reading the files into macros, because the x columns can be generated on the fly, and because reading directly from the files makes it possible to use a loop. Note I made a custom cycle list in the axis options that sets the colors for the plots.

As for the actual question, you can use nodes near coords to add the numbers on the bars. By default it prints the y-value, but custom text can be specified. E.g. with nodes near coords={foo}, the word foo will be printed. I used nodes near coords={#1}, where #1 represents the loop variable.

To add a number along the regression lines, you can append node[pos=x,anchor=south] {#1} to the end of the \addplot, before the closing ;. For this case pos=0.12 seems to work OK.

Regarding the legend. Assuming you want the regression lines plotted on top of the bars, the easiest way seems to be not using \addlegendentry, and instead adding

\legend{,,,,,,Hop-1,Hop-2,Hop-3,Hop-4,Hop-5,Hop-6}

That the first six entries in the comma separated list are empty means that no legend is made for the first six \addplots, which are the ones making the bars.

I also added some more comments in the code, please ask if anything is unclear.

enter image description here

\RequirePackage{filecontents}

\begin{filecontents*}{hop1}
1pkt/5sec 492
1pkt/10sec 483
1pkt/20sec 479
1pkt/30sec 420
1pkt/40sec 493
1pkt/50sec 500
\end{filecontents*}

\begin{filecontents*}{hop2}
1pkt/5sec 457
1pkt/10sec 409
1pkt/20sec 440
1pkt/30sec 50
1pkt/40sec 490
1pkt/50sec 498
\end{filecontents*}

\begin{filecontents*}{hop3}
1pkt/5sec 407
1pkt/10sec 438
1pkt/20sec 399
1pkt/30sec 322
1pkt/40sec 412
1pkt/50sec 497
\end{filecontents*}

\begin{filecontents*}{hop4}
1pkt/5sec 63
1pkt/10sec 156
1pkt/20sec 370
1pkt/30sec 331
1pkt/40sec 357
1pkt/50sec 496
\end{filecontents*}

\begin{filecontents*}{hop5}
1pkt/5sec 90
1pkt/10sec 206
1pkt/20sec 368
1pkt/30sec 323
1pkt/40sec 379
1pkt/50sec 490
\end{filecontents*}

\begin{filecontents*}{hop6}
1pkt/5sec 56
1pkt/10sec 240
1pkt/20sec 0
1pkt/30sec 324
1pkt/40sec 376
1pkt/50sec 484
\end{filecontents*}



\documentclass[conference]{IEEEtran}
\usepackage{pgfplotstable} % loads pgfplots
\pgfplotsset{compat=1.7}

\begin{document} 
\begin{figure*}
\begin{tikzpicture}
\begin{axis}[
  height=6cm, width=13.5cm,
  ybar,
  bar width=0.2cm,
  xlabel={Pkt Generation Rate},
  ylabel={Pkt Delivery Ratio},
  ymin=0, ymax=550, % I increased ymax to make room for the numbers on the bars
  symbolic x coords={1pkt/5sec,1pkt/10sec,1pkt/20sec,1pkt/30sec,1pkt/40sec,1pkt/50sec},
  xtick distance=1,
  ytick={0,100,200,300,400,500,600},
  legend style={font=\small},
  legend pos=outer north east,
  ymajorgrids=true,
  cycle list={ % set the six colors here
    {fill, draw, black!80!white},
    {fill, draw, blue!80!white},
    {fill, draw, red!80!white},
    {fill, draw, orange!80!white},
    {fill, draw, green!80!white},
    {fill, draw, cyan!80!white}
  },
  node near coords style={black,font=\footnotesize} % color and font for numbers on bars
]


\pgfplotsinvokeforeach{1,...,6}{% starts a loop
% the loop variable is represented by #1
  \addplot 
     +[nodes near coords={#1}, % adds the number above the bars
       draw=red!20!black % override the draw-color
      ] table {hop#1};

}


\pgfplotsinvokeforeach{1,...,6}{%
  \addplot 
     +[sharp plot,  % line plot
       line legend, % line legend
       line width=1pt,
      ] 
      table [
          % make the x-column
          create on use/x/.style={create col/expr={\pgfplotstablerow}},
          % do the regression
           y={create col/linear regression={x=x}}
            ] {hop#1}
         % add a node with the number (1,2,...,6)
         node[
           % this ifthenelse will set the anchor of the node to 90
           % (equivalent to north) for the fourth node), while the 
           % rest gets anchor=270 (equivalent to south). Hence the
           % fourth node is below the line, the rest are above.
           anchor={ifthenelse(#1==4,90,270)},
           % set padding between node and node border:
           inner sep=1pt,
           % set fractional distance along the lines
           % by trial and error I found that 0.12 looks OK
           % it places the numbers about halfway between the first
           % two groups of bars
           pos=0.12,
           % set font, color etc.
           font=\large
        ] {#1};

}

% add the legend entries
% by making the first six entries (which correspond to the bar plots)
% empty, you only get a legend for the regression lines
\legend{,,,,,,Hop-1,Hop-2,Hop-3,Hop-4,Hop-5,Hop-6}

\end{axis}
\end{tikzpicture}
\end{figure*}
\end{document} 
7
  • Thank you so much It worked great for me but can you please suggest me a little changes in it. 1) your code for adding a number on regression line is not working for me. 2) I want regression line labels only and want to discard labels for bars. You help in this regard would be appreciated, May 21, 2018 at 9:34
  • @TayyabaLiaqat 1) What do you mean by not working? 2) As I said, the numbers on the bars are created by nodes near coords, so just remove that option. May 21, 2018 at 9:43
  • I am talking about this line of code, "To add a number along the regression lines, you can append node[pos=x,auto] {#1} to the end of the \addplot, before the closing ;" that suggests changes for appending numbers along regression lines. P.S I want numbers written on regression lines as well as bars. Thank you. May 21, 2018 at 9:46
  • @TayyabaLiaqat I do not understand what you mean. If you look at the code in my answer I have added nodes like that, so there are numbers on the regression lines. Doesn’t that work for you? And make up your mind, in your first comment you said you didn’t want numbers on the bars, now you say you do want numbers on the bars. May 21, 2018 at 9:50
  • First- I do not want legends for bars but for regression lines only (I wrongly stated it as label which created confusion, my bad) Second- In the second version of code I want 1-6 numbers written on the straight lines too (regression lines). I hope it helps. May 21, 2018 at 10:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .