TransWikia.com

Barplots with Tikz and PGFPLOTS

TeX - LaTeX Asked by forecaster on March 29, 2021

I’m new to LaTeX. I’m trying to create a bar plot with beamer document class. Here is what I need:

enter image description here

Here is what I get in LaTeX:

enter image description here

Please see below for MWE:

Here are my questions:

  1. How can I replicate the above chart, similar looking in LaTeX ? I like to retain the tik marks and the out borders. I’m more interested in the X axis label and separation of the bars.
  2. How do I adjust the width of the chart with similar aspect ratio
    just to this chart (as I have several charts, I do not want give
    global options) and also the width of the bar to chart above?
documentclass{beamer}
setbeamertemplate{navigation symbols}{}
usetheme{Boadilla}
usepackage{pgfplots}
pgfplotsset{width=7cm,compat=1.8}


begin{document}
    
    begin{frame}
        begin{figure}
                centering
            begin{tikzpicture}
                begin{axis}[ybar=25pt,ymin=0,ymax=150,]
                
                    addplot[ybar,fill=blue, area legend] coordinates {(Rat,40)};
                    addplot[ybar,fill=red, area legend] coordinates {(Hippopotamus,146)};
                
                end{axis}
            end{tikzpicture}
        end{figure}
    end{frame}

end{document}

One Answer

Welcome to TeX.SE. This follows: PGFPLOTS - Create Bar plot with bars in different colors. See the code for the various changes.

enter image description here

documentclass{beamer}
setbeamertemplate{navigation symbols}{}
usetheme{Boadilla}
usepackage{pgfplots}
pgfplotsset{width=7cm,compat=1.8}

begin{document}

begin{frame}
  begin{figure}
  centering
    begin{tikzpicture}
       begin{axis}[
         ybar,
         xmin=0.5,xmax=2.5, %<-- added
         ymin=0,
         ymax=150,
         area legend,
         xtick={1,2}, % <-- added
         xticklabels={Rat,Hippopotamus}, % <-- added
         every axis plot/.append style={ % <-- added
          bar width=.5,
          bar shift=0pt,
          fill} 
         ]
         addplot[fill=blue] coordinates {(1,40)};
         addplot[fill=red] coordinates {(2,146)};
       end{axis}
    end{tikzpicture}
  end{figure}
end{frame}

end{document}

Correct answer by Ross on March 29, 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