2

I have this bar graph, and I want to do 3 things:

  1. Remove the numbers in the bars, they are overlapping (If possible keep only one, symbolizing that both bars have the same value);
  2. Within the frame of the legend, enter "Data Size" and then the values ​​of 10KB and 20KB;
  3. Change the y-axis value to 28

The code I'm using:

\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.3,
legend  style={at={(0.5 ,-0.20)},
anchor=north,legend  columns =-1},
ylabel ={Memory Usage (KB)},
xlabel ={Transmissions},
symbolic x coords ={10,100,250},
xtick=data,
nodes  near  coords ,
nodes  near  coords  align ={vertical},
]
\addplot  coordinates  {(10,25.4) (100,25.4) (250,25.4)};
\addplot  coordinates  {(10,25.4) (100,25.4) (250,25.4)};
\legend{10KB,20KB}
\end{axis}
\end{tikzpicture}

The graph I have is this:

enter image description here

Mutante
  • 245

2 Answers2

4
\documentclass[border = 5pt]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ymin = 0, ymax = 28,
    ybar,
    enlarge x limits=0.3,
    legend  style={at={(0.5 ,-0.20)},
    anchor=north,legend  columns =-1},
    ylabel ={Memory Usage (KB)},
    xlabel ={Transmissions},
    symbolic x coords ={10,100,250},
    xtick=data,    
    nodes  near  coords  align ={vertical},
  ]

  \addlegendimage{empty legend}
  \addplot[nodes  near  coords, fill = blue!30]  coordinates  {(10,25.4) (100,25.4) (250,25.4)};
  \addplot  coordinates  {(10,25.4) (100,25.4) (250,25.4)};

  \addlegendentry{Data Size}
  \addlegendentry{100KB}
  \addlegendentry{20KB}
  %\legend{10KB,20KB}
  \end{axis}
\end{tikzpicture}
\end{document}
  1. Remove nodes near coords

  2. Use \addlegendentry{Data Size}

  3. Set the limit with ymax

enter image description here

caverac
  • 7,931
  • 2
  • 15
  • 31
  • But how can i display only one value in the bars? See the answer from @marmot. – Mutante Dec 29 '18 at 18:50
  • @Mutante Add the option to just one of the plots \addplot[nodes near coords, fill = blue!30] coordinates {(10,25.4) (100,25.4) (250,25.4)}; – caverac Dec 29 '18 at 18:58
2

This is just a small addendum to caverac's nice answer that shows how one may merge two nodes near coords into one.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\newcounter{mynodeindex}
\pgfplotsset{%based on https://tex.stackexchange.com/a/75811/121799
    step nodeindex/.code={\stepcounter{mynodeindex}},
    name nodes near coords/.style={
        every node near coord/.append style={/pgfplots/step nodeindex,
            name=#1-\themynodeindex,
            alias=#1-last,
        },
    },
    name nodes near coords/.default=coordnode
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
enlargelimits=0.3,
legend  style={at={(0.5 ,-0.20)},
anchor=north,legend  columns =-1},
ylabel ={Memory Usage (KB)},
xlabel ={Transmissions},
symbolic x coords ={10,100,250},
xtick=data,
nodes  near  coords={},
nodes  near  coords  align ={vertical},
name nodes near coords
]
\addplot  coordinates  {(10,28) (100,28) (250,28)};
\addplot  coordinates  {(10,28) (100,28) (250,28)};
\legend{10KB,20KB}
\end{axis}
\pgfmathtruncatemacro{\Xmax}{\themynodeindex/2}
\foreach \X [evaluate=\X as \Y using {int(\X+\Xmax)}]  in {1,...,\Xmax}
{\path (coordnode-\X) -- (coordnode-\Y) node[midway,yshift=2pt] {28};}
\end{tikzpicture}
\end{document}

enter image description here