LaTeX forum ⇒ Graphics, Figures & Tablescropped ybar plot Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
Linguist
Posts: 41
Joined: Mon Nov 07, 2011 12:07 pm

cropped ybar plot

Postby Linguist » Tue Dec 08, 2020 6:15 am

Hi All,

The bar plot produced by the following code has two issues:
1) It is cropped at the extreme left and right edges
2) There is too much space between the two groups of bars.

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{minimal}
  2. \usepackage{pgfplots}
  3. \pgfplotsset{compat=1.17}
  4.  
  5. \begin{document}
  6.  
  7. \begin{tikzpicture}
  8. \begin{axis}[
  9. ybar,
  10. symbolic x coords={Overall,Specific},
  11. xtick=data,
  12. yticklabel={\pgfmathprintnumber{\tick}\%},
  13. ymin=0,
  14. ymax=100,
  15. ymajorgrids=true,
  16. ]
  17. \addplot coordinates {(Overall,45) (Specific,70)};
  18. \addplot coordinates {(Overall,29) (Specific,16)};
  19. \addplot coordinates {(Overall,26) (Specific,15)};
  20. \legend{type1,type2,type3}
  21. \end{axis}
  22. \end{tikzpicture}
  23.  
  24. \end{document}


This person has a similar issue here: https://tex.stackexchange.com/questions/460707/horizontal-chart-bars-are-cropped But the first solution (using enlargelimits) just adds space below the ybars for my plot and the spacing/cropping issues remain.

The equivalent to the second solution (setting xmin and xmax with [normalized]) indeed changes where the bars are placed , but the principle behind normalized is not explained so I am left randomly guessing numbers to try and approximate an acceptable output.

Bartman
Posts: 164
Joined: Fri Jan 03, 2020 2:39 pm

Postby Bartman » Tue Dec 08, 2020 2:48 pm

Section 4.14.1 "Configuration of Limits Ranges" of the manual not only lists the enlargelimits option. Other options allow you to change the size for only one axis.

Alternatively, you could avoid the symbolic coordinates and use numbers for the x-axis, while the labeling is done using the xticklabels option.

Linguist
Posts: 41
Joined: Mon Nov 07, 2011 12:07 pm

Postby Linguist » Wed Dec 09, 2020 12:57 am

Thanks for pointing me in the right direction.

I've found using enlarge x limits={abs=<value>} combined with specifying the width of the plot allows me to position the bars with more control and get a better result.

An explanation of how
Bartman wrote:avoiding the symbolic coordinates and use numbers for the x-axis, while the labeling is done using the xticklabels option
might be useful for others. (I can't figure it out) But for future reference I'm putting the updated code below.

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{minimal}
  2. \usepackage{pgfplots}
  3. \pgfplotsset{compat=1.17}
  4.  
  5. \begin{document}
  6.  
  7. \begin{tikzpicture}
  8. \begin{axis}[
  9. ybar,
  10. symbolic x coords={Overall,Specific},
  11. xtick=data,
  12. yticklabel={\pgfmathprintnumber{\tick}\%},
  13. ymin=0,
  14. ymax=100,
  15. ymajorgrids=true,
  16. width=6cm, %added to specify the width so that the gap between each group of bars is reduced
  17. enlarge x limits={abs=1cm} %added to `move' each group of bars 1cm away from the edge of the plot
  18. ]
  19. \addplot coordinates {(Overall,45) (Specific,70)};
  20. \addplot coordinates {(Overall,29) (Specific,16)};
  21. \addplot coordinates {(Overall,26) (Specific,15)};
  22. \legend{type1,type2,type3}
  23. \end{axis}
  24. \end{tikzpicture}
  25.  
  26. \end{document}


The legend in the above bar chart partially covers up the first bar in the second group, but that can be fixed with legend pos=north west, or various other options covered in §4.9.5 of the pgfplots manual


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 5 guests