Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I just started using TikZ and I am trying to make the following bar diagram:

The bar diagram

My main problem is to define bar type in which I could put the following arguments (for every bar separately!): (1) the height on which the bar starts, (2) the height of the bar, (3) the distance of the "nodes near coords" marks above the bars,

or find something already implemented that simulates that.

In this way, I would be able to make my graph in three "levels" (or three graphs in one), as the picture shows.

Another problem are those vertical lines that do not overlap with bars and the numbers above them.

What I have so far is this:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}


\pgfplotsset{mystyle/.style=
{
font=\tiny,
ybar,
bar shift={0pt},
xtick={1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25,3.5,3.75,4,4.25,4.5,4.75,5,5.25},
ytick=\empty,
xmin=0.75,
xmax=5.25,
ymin=0,
ymax=100,
bar width=10,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}},
}}

\begin{tikzpicture}
\begin{axis}[mystyle]
\addplot [draw=green, fill=green] plot coordinates
{(1,1) (1.5,2) (2.5,3) (3, 7) (3.5, 10) (4, 17) (4.5, 18) (5,8)};
\end{axis}
\end{tikzpicture}

\end{document}

Any help would be very appreciated!

share|improve this question
    
I'm not sure this is such a good idea. Not including the base lines for each of the individual plots is pretty confusing. I think it would be best to simply have three complete axes. Or is there a specific reason for this kind of arrangement? –  Jake Oct 28 '13 at 8:36
    
@Jake Thank for the comment! I guess you're right. There is no specific reason for the kind of arrangement I described. It's just that I lack experience in using tikz and pgfplot, and thought that would be the simplest way to go. How could I make three x-axes in one picture? I would have to lift the other two x-axes to some height somehow, right? –  Zvonimir Oct 28 '13 at 10:16
    
@Jake I think I know how to make "levels"! If I include this code \begin{axis}[mystyle,ymin=-50] \addplot [draw=green, fill=green] plot coordinates {(1,1) (1.5,2) (2.5,3) (3, 7) (3.5, 10) (4, 17) (4.5, 18) (5,8)}; \end{axis} to the tikz picture as above, I will get "two layers". I.e., this code makes a copy of the bar diagram I made, only it starts on height 50, which is what I wanted. Now I just adjust the colour and numbers as they are in the original picture. Thanks for the tip Jake! Any ideas on how to draw vertical lines which "go under" bars and labels? –  Zvonimir Oct 28 '13 at 10:40
    
I finished it entirely. I will post my solution as soon as I polish it (make it more readable). Thanks again for your help Jake! –  Zvonimir Oct 28 '13 at 14:01

1 Answer 1

up vote 2 down vote accepted

Here is what I did. Inside one tikzpicture environment I put three axis environments and modified their y-axis range (i.e. ymin and ymax) to translate these coordinate systems according to my wishes.

Here is the code (with some comments on several details not essential to my question, but might help to better understand the picture):

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{fix-cm} % package that enables all font sizes for Computer Modern font; not essential in this example

\begin{document}

\fontsize{3pt}{6pt}

\pgfplotsset{mystyle/.style=
{
ybar,
xtick={1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25,3.5,3.75,4,4.25,4.5,4.75,5},
x tick label style={major tick length=2pt, tick style=white}, % this makes ticks white (i.e. invisible on white surface), but I want to keep them in order to be able to manipulate the space between the x-axis and the numbers
ytick=\empty,
nodes near coords,
xmin=0.75,
xmax=5.25, % y-dimensions will be determined for each axis environment separately 
bar width=9pt, 
line width=0.1pt,
}}


\begin{tikzpicture}
\begin{axis}[mystyle, ymin=-5,ymax=95, grid=both,] % the "grid=both" part draws grids for both axes; as y-axis doesn't have ticks, grids are not drawn;
%it is neccessary to put grid command here, and not in the definition of mystyle, to avoid that the grids cross other bars 
\addplot [draw=black, fill=green] plot coordinates
{(1,1) (1.5,2) (2.5,3) (3, 7) (3.5, 10) (4, 17) (4.5, 18) (5,8)};
\end{axis}


\begin{axis}[mystyle, ymin=-35,ymax=65,]
\addplot [draw=black, fill=blue] plot coordinates
{(1,32) (1.5,16) (2,13) (2.5,4) (5,1)};
\end{axis}


\begin{axis}[mystyle, ymin=-80,ymax=20,]
\addplot [draw=black, fill=yellow] plot coordinates
{(1,5) (1.25,9) (1.5,13) (1.75,14) (2,12) (2.25,6) (2.5,2) (2.75,1) (3, 1) (3.25,1) (3.5, 1) (4, 1)};
\end{axis}


\end{tikzpicture}

\end{document}

and here is the picture enter image description here

The point is that if you set ymin=-5 in an axis environment, x-axis will be drawn at height -5. Since bars are drawn from height 0 to some height given by the user, the bars will be positioned 5 units above the x-axis.

In order to explicitly see what I mean, you can put ytick={}, to the axis environment whose y-axis coordinates you want to see (you put this inside the square brackets).

I hope this (not especially elegant) idea helps someone! Cheers!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.