Graphics, Figures & TablesModifying axis names in bar plots

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Modifying axis names in bar plots

Post by jhapk »

Hi,

how can I change the lables of the x-axis as shown in the attached bar-plot in tikz or pgfplot?

Thanks
Attachments
Mass_contribution_of_each_species.jpg
Mass_contribution_of_each_species.jpg (23.14 KiB) Viewed 4243 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Modifying axis names in bar plots

Post by localghost »

See Section 4.14 (Tick options, p. 158ff) of the pgfplots manual (keyword: xticklabels).


Thorsten
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Modifying axis names in bar plots

Post by jhapk »

Hi

I wrote the following code:

Code: Select all

\documentclass{minimal}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
  \begin{axis}[ybar]
    xticklabels={$N2$,$O2$,$H20 $,$CO2 $,$CH4 $,$CO $,$OH $,$O $,$CH2O $,$H2$}
    xticks={0,1,2,3,4,5,6,7,8,9}

  \addplot[draw=blue,fill=blue] plot
  coordinates {
    (0,42.51) 
    (1,35.98) 
    (2,10.41) 
    (3,5.49) 
    (4,2.38) 
    (5,0.87) 
    (6,0.6) 
    (7,0.49) 
    (8,0.38)
    (9,0.317)
  };
\end{axis}
\end{tikzpicture}
\end{document}
But it is giving me the following output
Screenshot.png
Screenshot.png (9.2 KiB) Viewed 4227 times
What am I doing wrong?
Last edited by jhapk on Thu Jul 29, 2010 11:35 pm, edited 1 time in total.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Modifying axis names in bar plots

Post by localghost »

I don't think that you tested the example you provided. It doesn't work at all. So you still have to learn how to build a minimal working example (MWE).

Code: Select all

\documentclass{article}
\usepackage{pgfplots}
\usepackage[version=3]{mhchem}

\pagestyle{empty}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[%
      font=\scriptsize,
      width=10cm,height=7cm,
      xtick={0,1,...,9},
      xticklabels={\ce{N2},\ce{O2},\ce{H2O},\ce{CO2},\ce{CH4},\ce{CO},\ce{OH},\ce{O},\ce{CH2O},\ce{H2}},
      ylabel={Percentage contribution to total mass (\%)},
      extra description/.code={\node[left] at (0.95,0.9) {Total contribution$=$99,427\%};}
    ]
      \addplot[ybar,draw=blue,fill=blue] plot coordinates {%
        (0,42.51)
        (1,35.98)
        (2,10.41)
        (3,5.49)
        (4,2.38)
        (5,0.87)
        (6,0.6)
        (7,0.49)
        (8,0.38)
        (9,0.317)
      };
    \end{axis}
  \end{tikzpicture}
\end{document}
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Re: Modifying axis names in bar plots

Post by jhapk »

Hi,

sorry about the error in my code. I will be more careful from hereon.

One more question, How do I turn off the yticks and change the spacing of Ylabel?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Modifying axis names in bar plots

Post by localghost »

jhapk wrote:[...] How do I turn off the yticks and change the spacing of Ylabel?
This can be answered with a nice acronym: RTFM. But since we have come so far we just supplement the code from above and present it here.

Code: Select all

\documentclass{article}
\usepackage{pgfplots}
\usepackage[version=3]{mhchem}

\pagestyle{empty}

\pgfplotsset{%
  every axis y label/.style={%
    at={(0,0.5)},
    xshift=-10pt,
    rotate=90
  }
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[%
      font=\scriptsize,
      width=10cm,height=7cm,
      xtick={0,1,...,9},
      ytick=\empty,
      xticklabels={\ce{N2},\ce{O2},\ce{H2O},\ce{CO2},\ce{CH4},\ce{CO},\ce{OH},\ce{O},\ce{CH2O},\ce{H2}},
      ylabel={Percentage contribution to total mass (\%)},
      extra description/.code={\node[left] at (0.95,0.9) {Total contribution$=$99,427\%};}
    ]
      \addplot[ybar,draw=blue,fill=blue] plot coordinates {%
        (0,42.51)
        (1,35.98)
        (2,10.41)
        (3,5.49)
        (4,2.38)
        (5,0.87)
        (6,0.6)
        (7,0.49)
        (8,0.38)
        (9,0.317)
      };
    \end{axis}
  \end{tikzpicture}
\end{document}
Strangely I couldn't do the settings for the position of the Y axis label locally (with version 1.2.2). But perhaps I did something wrong.
jhapk
Posts: 81
Joined: Tue Apr 20, 2010 9:33 pm

Re: Modifying axis names in bar plots

Post by jhapk »

Thanks a lot Localghost. That worked.
Post Reply