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 am trying to draw a graph with percentage in x-axis and categories on y-axis. I cannot make the graph to show % sign. Here is my MWE:

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

\pagestyle{empty}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    axis lines*=left,
    title=Histogram of S\&P 500 CEOs Past Experience,
    xbar,
    width=8cm,
    height=5cm,
    xlabel={},
    symbolic y coords={Sales, Marketing, Finance, Operations},
    ytick=data,
    xmin=0,
    xmax=0.5, 
    nodes near coords,
    nodes near coords align={horizontal}
    ]

  \addplot
[draw=blue,pattern=horizontal lines light blue] 
coordinates
     {(0.17,Sales)  (0.24,Marketing) (0.31,Finance) (0.42,Operations)};
\end{axis}
\end{tikzpicture}
\end{document}

as you can see, I can make the graph with decimal number and ideally I'd like to have % sign. Any suggestion?

share|improve this question
2  
Where do you want to show the % sign? After every x tick label? On the bar labels? Also, in your real application, are you only going to show four or five bars? In that case, you might want to get rid of the x axis, since all the needed information is already in the bar labels. –  Jake Dec 17 '12 at 22:23
    
@Jake: Yeah, I would like to show % sign after each label. I mean 17% instead of 0.17, 24% instead of 0.24 etc... –  N Nik Dec 17 '12 at 22:28

2 Answers 2

up vote 10 down vote accepted

You can use point meta to perform the multiplication by 100 and

nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}

to add the percentage sign:

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

\pagestyle{empty}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    axis lines*=left,
    title=Histogram of S\&P 500 CEOs Past Experience,
    xbar,
    width=8cm,
    height=5cm,
    xlabel={},
    symbolic y coords={Sales, Marketing, Finance, Operations},
    ytick=data,
    xmin=0,
    xmax=0.5, 
    point meta={x*100},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
    nodes near coords align={horizontal}
    ]
  \addplot
[draw=blue,pattern=horizontal lines light blue] 
coordinates
     {(0.17,Sales)  (0.24,Marketing) (0.31,Finance) (0.42,Operations)};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

If you also want this formatting for the x tick labels, then you can use

xticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%},

(no need to explicitly give the labels):

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

\pagestyle{empty}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    axis lines*=left,
    title=Histogram of S\&P 500 CEOs Past Experience,
    xbar,
    width=8cm,
    height=5cm,
    xlabel={},
    symbolic y coords={Sales, Marketing, Finance, Operations},
    ytick=data,
    xmin=0,
    xmax=0.5, 
    xticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%},
    point meta={x*100},
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
    nodes near coords align={horizontal},
    ]
  \addplot
[draw=blue,pattern=horizontal lines light blue] 
coordinates
     {(0.17,Sales)  (0.24,Marketing) (0.31,Finance) (0.42,Operations)};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer

You can use xticklabel={\pgfmathprintnumber\tick\%}.:

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

\pagestyle{empty}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    axis lines*=left,
    title=Histogram of S \& P 500 CEOs Past Experience,
    xbar,
    width=8cm,
    height=5cm,
    xlabel={},
    symbolic y coords={Sales, Marketing, Finance, Operations},
    ytick=data,
    xmin=0,
    xmax=100, 
    nodes near coords,
    nodes near coords align={horizontal},
    xtick={20,40,60,80,100},
    xticklabel=
{\pgfmathprintnumber\tick\%}
    ]

  \addplot
[draw=blue,pattern=horizontal lines light blue] 
coordinates
     {(17,Sales)  (24,Marketing) (31,Finance) (42,Operations)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.