10

I'd like to format the nodes in this bar chart with $ signs (e.g $275). (The sets of bars could also be closer together; perhaps that should be another question.)

The answer is probably out there somewhere but my searches haven't found it.

enter image description here

Here's my MWE:

%http://tex.stackexchange.com/questions/88892/pgfplots-bar-plot-spacing-inbetween-bars
%http://tex.stackexchange.com/questions/113241/pgfplots-grouped-bar-chart-from-file
%http://tex.stackexchange.com/questions/35872/multiline-coords

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{calc,decorations.pathmorphing}

%deal with warning message in log
\pgfplotsset{compat=1.8}

\usepackage{helvet}
\usepackage[eulergreek]{sansmath}

\begin{document}
\newcommand{\mywidth}{12cm}
\newcommand{\myheight}{5cm}

\pgfplotstableread[col sep=comma]{
seats, 2003, 2008
ordinary, 42.26, 52.16
premium, 275, 325
}\ticketprices

\begin{tikzpicture}[font=\sffamily\sansmath]

\begin{axis}[ 
      ybar=10pt,
      bar width=20pt,
      ymin=0,
      tick style={draw=none},
      width=\mywidth, height=\myheight, 
      title={Tickets at Fenway Park}, 
      hide y axis,
      axis x line*=bottom,
      xtick=data,
      xticklabel style={align=center},
      xticklabels={
        2003 2008\\
        ordinary seats,
        2003 2008\\
        premium seats
      },
      nodes near coords,
    ]
    \addplot [fill=gray] table[x expr=\coordindex, y=2003]  {\ticketprices};
    \addplot [fill=black] table[x expr=\coordindex, y=2008] {\ticketprices};
\end{axis} 
\end{tikzpicture}
\end{document}
  • nodes near coords={\$\pgfmathprintnumber... ;) – percusse Jun 19 '13 at 16:11
12

You can decide what to print in the nodes near coords labels using the optional argument. By default, it's defined as nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}, so it's a pretty formatting of the value of \pgfplotspointmeta, which, by default, holds the current y value.

In your case, you could set

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

to put a dollar sign in front of each value.

To make the plot width match the number of data series, the easiest thing to do is to define the x unit vector in terms of the bar width: If your bar width is 20pt, and the gap between the bars is ybar=15pt, you could set x={2*15pt+2*20pt+10pt} (two data series, two gaps, plus a little extra to separate the groups. To make sure the bars aren't cut off at the edges, you can set enlarge x limits={abs=20pt+15pt}, (space for one bar, plus another gap).

The solution scales as more plots are added:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{calc,decorations.pathmorphing}

%deal with warning message in log
\pgfplotsset{compat=1.8}

\usepackage{helvet}
\usepackage[eulergreek]{sansmath}

\begin{document}
\newcommand{\mywidth}{12cm}
\newcommand{\myheight}{5cm}

\pgfplotstableread[col sep=comma]{
seats, 2003, 2008
ordinary, 42.26, 52.16
premium, 275, 325
extreme, 800, 850
}\ticketprices

\begin{tikzpicture}[font=\sffamily\sansmath]

\begin{axis}[ 
      ybar=15pt,
      bar width=20pt,
      x={2*15pt+2*20pt+10pt},
      enlarge x limits={abs=20pt+15pt},
      ymin=0,
      tick style={draw=none},
      width=\mywidth, height=\myheight, 
      title={Tickets at Fenway Park}, 
      hide y axis,
      axis x line*=bottom,
      xtick=data,
      xticklabel style={align=center},
      xticklabels={
        2003 2008\\
        ordinary seats,
        2003 2008\\
        premium seats,
        2003 2008\\
        extreme deluxe
      },
      nodes near coords={\$\pgfmathprintnumber{\pgfplotspointmeta}},
    ]
    \addplot [fill=gray] table[x expr=\coordindex, y=2003]  {\ticketprices};
    \addplot [fill=black] table[x expr=\coordindex, y=2008] {\ticketprices};
\end{axis} 
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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