6

I am trying to generate a histogram like the following:

enter image description here

While going through the examples pgf plots, I was able to generate a histogram similar to the following:

enter image description here

using the code from the example link shared above:

    \documentclass{article}
    \usepackage{csvsimple}
    \usepackage{longtable}
    \usepackage{xcolor}
    \usepackage{booktabs}
    \usepackage{caption}
    \usepackage{pgfplots}



    \begin{document}
    \begin{tikzpicture}
    \begin{axis}[
        x tick label style={
            /pgf/number format/1000 sep=},
        ylabel=Year,
        enlargelimits=0.05,
        legend style={at={(0.5,-0.1)},
        anchor=north,legend columns=-1},
        ybar interval=0.7,
    ]
    \addplot 
        coordinates {(2012,408184) (2011,408348)
            (2010,414870) (2009,412156)};
    \addplot 
        coordinates {(2012,388950) (2011,393007) 
            (2010,398449) (2009,395972)};
    \legend{Men,Women}
    \end{axis}
    \end{tikzpicture}
    \end{document}

Is there a way with pgf plots, where I could generate custom looking histogram?

4
  • How about rounded corners: \addplot[rounded corners=8pt,fill=blue!60!black] coordinates {(2012,408184) (2011,408348) (2010,414870) (2009,412156)}; \addplot[rounded corners=3pt,fill=red!60!black] coordinates {(2012,388950) (2011,393007) (2010,398449) (2009,395972)};?
    – user121799
    Apr 30, 2019 at 12:29
  • @marmot, good idea, however the legend is than screwy ... can you fix this?
    – Zarko
    Apr 30, 2019 at 12:38
  • @marmot Could we also remove the y axis and have the corresponding values at the top of histogram?
    – Amanda
    Apr 30, 2019 at 12:47
  • @Zarko Yes. It took me a while to realize that the OP uses ybar interval rather than ybar, where I was stuck... ;-)
    – user121799
    Apr 30, 2019 at 13:18

1 Answer 1

9

This is a simple-minded proposal. Note that I changed from ybar interval to ybar, see here. The main idea is to use rounded corners but then one has to fix the legend to have a smaller radius of the rounded corners. I also removed the y axis and added nodes near coords, as requested.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}


\begin{document}
\begin{tikzpicture}
\pgfplotsset{/pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={
        \draw [##1,/tikz/.cd,rounded corners=1pt,bar width=3pt,yshift=-0.2em,bar shift=0pt]
        plot coordinates {(0cm,0.8em) (2*\pgfplotbarwidth,0.6em)};
    },
},}         
\begin{axis}[hide y axis,axis x line=bottom,
    x tick label style={
        /pgf/number format/1000 sep=},
    %ylabel=Year,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.1)},        anchor=north,legend columns=-1},
    ybar,bar width=2em,
    %symbolic x coords={2009,2010,2011,2012},
    ybar legend,
    nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta},
    every node near coord/.append style={
    anchor=mid west,
    rotate=70} % from https://tex.stackexchange.com/a/27143/121799
]
\addplot[rounded corners=8pt,fill=blue!60!black,
] 
    coordinates {(2009,412156) (2010,414870) (2011,408348) (2012,408184)};
\addplot [rounded corners=8pt,fill=red!60!black]  
    coordinates {(2009,395972) (2010,398449) (2011,393007) (2012,388950)};
\legend{Men,Women}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • Could we also remove the y axis and have the corresponding values at the top of each bar?
    – Amanda
    Apr 30, 2019 at 12:51
  • thank you very much! very good answer! (+1)
    – Zarko
    Apr 30, 2019 at 13:22

You must log in to answer this question.

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