1

I want to draw a graph like shown in the image, but my values will not start from 0%(see image).

For example: I want to plot Men in 'All' from 20% -60% (means slab will start from 20% and will end at 60% in blue color) and Woman in 'All' from 30% to 80% (means slab will start from 30% and will end at 80% in red color) for the shared region 20%-30%, it should be in shaded color of blue and red.

Please suggest me how to do it in LaTex or in any other graph editor. Thanks!

enter image description here

2
  • Could you post your MWE on what you have tried to do so far? IMO, it is much more useful than an image. May 24, 2017 at 9:27
  • @RaajaG I'm not getting how should I start, I go through with that link tex.stackexchange.com/questions/318154/… . This is the most relevant to what I want but not exactly. Actually, I'm not getting whether it is bar graph or not? If anybody knows please tell me to which category, the graph I described belong ? May 24, 2017 at 9:38

1 Answer 1

2

It is a bar graph of sorts I guess. Normally I'd use pgfplots for that, but as I don't know how to do what you request, I show an option where the plot is built manually with plain TikZ. Because of that, it is probably less flexible than what a pgfplots method would be. The overlap is handled by drawing both bars with 50% opacity.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplotstable}
\pgfplotstableread{ % save data to table
Mmin Mmax Wmin Wmax label
20 60 30 80 All
10 50 30 70 completed
20 90 60 100 dropped
}\datatable
% get number of rows
\pgfplotstablegetrowsof{\datatable}
% subtract 1 because table indices start at 0
\pgfmathsetmacro{\Nrows}{\pgfplotsretval-1}
\begin{document}
\begin{tikzpicture}[
  bars/.style={draw,line width=5mm,opacity=0.5},
  women/.style={bars,color=red},
  men/.style={bars,color=blue},
  axis/.style={-stealth,thick},
  y=3cm
  ]
\foreach [count=\X from 1] \i in {0,...,\Nrows} {
  % get values from table
  \pgfplotstablegetelem{\i}{Mmin}\of\datatable \pgfmathsetmacro{\Mmin}{\pgfplotsretval/100}
  \pgfplotstablegetelem{\i}{Mmax}\of\datatable \pgfmathsetmacro{\Mmax}{\pgfplotsretval/100}
  \pgfplotstablegetelem{\i}{Wmin}\of\datatable \pgfmathsetmacro{\Wmin}{\pgfplotsretval/100}
  \pgfplotstablegetelem{\i}{Wmax}\of\datatable \pgfmathsetmacro{\Wmax}{\pgfplotsretval/100}

  % draw bars
  \path [men] (\X,\Mmin) -- (\X,\Mmax);
  \path [women] (\X,\Wmin) -- (\X,\Wmax);

  % add labels below x-axis
  \pgfplotstablegetelem{\i}{label}\of\datatable
  \node [rotate=40,below,anchor=north east,font=\footnotesize] at (\X,0) {\pgfplotsretval};
}

% draw axes
\draw [axis] (0,-0.1cm) -- (0,1.1);
\draw [axis] (-0.1cm,0) -- (\Nrows+2,0);

% make ticks on y-axis
\foreach [evaluate=\y as \Y using int(round(\y*100)))] \y in {0.2,0.4,...,1}
  \draw (2pt,\y) -- (-2pt,\y) node[left] {\Y \%};

% legend
\path [women] (\Nrows+2,0.7) -- ++(3mm,0) node[right,opacity=1,black] {Women};
\path [men] (\Nrows+2,0.5) -- ++(3mm,0) node[right,opacity=1,black] {Men};
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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