TransWikia.com

Incorrect bar graph formatting in TeX

TeX - LaTeX Asked by Egor Stoyan on November 6, 2021

For some reason, two very simple Bar graphs look very different after compiling in tex. More precisely, on the right graph there is a distance from the columns to the lower border of the square. In the left graph, the columns are adjacent to the lower border of the square. I would like to change the left figure, to have the distance between the bottom of the square and the blue columns on the left chart, as it is implemented on the right. Do you know how to change the code of the left chart?

Picture

begin{figure}[H]
begin{minipage}[h]{0.49linewidth}
begin{tikzpicture}
begin{axis}[
    ybar,
    enlargelimits=0.15,
    legend style={at={(0.75,-1)},
      anchor=north,legend columns=-1},
    symbolic x coords={1,2,3,4,5,6},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical}
    ]
addplot coordinates {(1, 179) (2, 122) (3, 79) (4, 59) (5, 49) (6, 205) };
end{axis}
end{tikzpicture}
end{minipage}
hfill
begin{minipage}[h]{0.49linewidth}
begin{tikzpicture}
begin{axis}[
    ybar,
    enlargelimits=0.15,
    legend style={at={(0.75,-1)},
      anchor=north,legend columns=-1},
    symbolic x coords={1,2,3,4,5,6},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical}
    ]
addplot coordinates {(1, 125) (2, 106) (3, 27) (4, 49) (5, 12) (6, 5) };
end{axis}
end{tikzpicture}
end{minipage}
end{figure}

One Answer

Try the following:

documentclass[margin=3mm]{standalone}
usepackage{pgfplots}
pgfplotsset{width=7cm,compat=1.17}
usetikzlibrary{pgfplots.groupplots}

begin{document}
    begin{tikzpicture}
begin{groupplot}[
    group style={group size=2 by 1},
    ybar,
    symbolic x coords={1,2,3,4,5,6},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical}
                    ]
nextgroupplot[enlarge y limits=0.4, ymax=250]
addplot coordinates {(1, 179) (2, 122) (3, 79) (4, 59) (5, 49) (6, 205) };
%
nextgroupplot[enlarge y limits=0.166,ymax=150]%0.166=250/150
addplot coordinates {(1, 125) (2, 106) (3, 27) (4, 49) (5, 12) (6, 5) };
end{groupplot}
    end{tikzpicture}
end{document}

enter image description here

  • As you can see, in MWE is used groupplot where are in each group defined ymax and enlarge y limits. Later are in ration of used ymax.
  • In above MWE are not defined legen styles, since in your MWE legends are not used.

Answered by Zarko on November 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP