6

I'm having trouble replicating this graph

graph

I know it's silly to do a graph with so little info, but my teacher demands it.. go figure...

I can't seem to make the bars stick together. It's my first attempt at TikZ ever...

what I've done so far:

\documentclass{article}  
\usepackage{pfgplots}
\begin{document}  
    \begin{tikzpicture}     
     \begin{axis}[ symbolic x coords={18S, HNF1, Normalização dos valores para 18S}, xtick=data]  
    \addplot[ybar,fill=black] coordinates { 
      (18S,42) 
      (HNF1,50) 
      (Normalização dos valores para 18S,80) 
 }; 
    \addplot[ybar,fill=gray] coordinates { 
      (18S,50) 
      (HNF1,30) 
      (Normalização dos valores para 18S, 100) 
 };  
  \end{axis}   
\end{tikzpicture}  
\end{document}
3
  • 1
    Could you please post what you have so far. That way we don't have to start from scratch.
    – Caramdir
    Commented Feb 19, 2011 at 23:56
  • \begin{tikzpicture} \begin{axis}[ symbolic x coords={18S, HNF1, Normalização dos valores para 18S}, xtick=data] \addplot[ybar,fill=black] coordinates { (18S,42) (HNF1,50) (Normalização dos valores para 18S,80) }; \addplot[ybar,fill=gray] coordinates { (18S,50) (HNF1,30) (Normalização dos valores para 18S, 100) }; \end{axis} \end{tikzpicture} this is more or less the example in the manual, I've been wrapping my head around the manual for some time now, guess this will take some time and patience
    – mariachi
    Commented Feb 20, 2011 at 0:33
  • Santiago, you should make this into a complete small document (with a \documentclass{article} and begin{document} etc.) and edit your question to include it, instead of adding the code as a comment.
    – Alan Munn
    Commented Feb 20, 2011 at 0:46

1 Answer 1

10

There are a number of issues with the code you posted:

  • By specifying ybar in each \addplot command, you cause the bars to be put on top of each other. Specify the ybar once in the \axis command.
  • the ybar (or xbar) allows a value which determines the separation between bars at the same x (or y) tick. Set this to 0 to get the bars adjacent.
  • Symbolic axes don't like accented characters as far as I can tell. So the way around this problem (which also makes entering the data easier), is to use an arbitrary symbolic label like {A,B,C} and then use xticklabels to supply the labels. This also allows you to put the long label into a tabular environment, so it will wrap correctly.)
  • To get the long label to fit you need to enlarge the limits.

Here's a new version of your plot: (by the way, your numbers don't match your picture!)

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots,amsmath,calc}
\usetikzlibrary{patterns}
\pgfplotsset{width=7cm,compat=1.3}

\begin{document}


\begin{tikzpicture}     
  \begin{axis}[ybar=0pt,
    symbolic x coords={A, B, C}, 
    xtick={A, B, C}, 
    xticklabels={18S, HNF1,
       \begin{tabular}{c}Normalização\\dos valores\\para 18S\end{tabular}},
    enlargelimits=0.3,bar width=20pt]  
    \addplot[fill=black] coordinates { 
      (A,42) 
      (B,50) 
      (C,80) 
 }; 
    \addplot[fill=gray] coordinates { 
      (A,50) 
      (B,30) 
      (C, 100) 
 };  
  \end{axis}   
\end{tikzpicture} 



\end{document}

enter image description here

4
  • Thank you very much! you saved my essay with that 0 hehe. I think I can do the rest.. thanks again :D
    – mariachi
    Commented Feb 20, 2011 at 2:25
  • @Santiago I hope your essay wasn't "reproduce this graph using tikZ" :-) Since the answer solved your problem you should click on the checkmark icon to accept the answer.
    – Alan Munn
    Commented Feb 20, 2011 at 2:43
  • I wish... and sorry, I'm new here and am still getting familiar with the interface. thanks again ;)
    – mariachi
    Commented Feb 20, 2011 at 2:46
  • @Santiago No need to apologize, and welcome. It's sometimes good to wait for a few answers before accepting an answer anyway, since there may be multiple good answers to certain questions. But in this case I think your problem was solved quite directly. Accepting answers is very helpful to others who might have a similar question since they can quickly see that the answer worked.
    – Alan Munn
    Commented Feb 20, 2011 at 2:55

You must log in to answer this question.

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