2

I have plotted a pgfplot bar graph using data from a csv file. The data has numbers in the hundreds of millions and the graph y axis is automatically scaled and shows the scale in scientific notation. Is it possible to change this notation to text? As in show (millions) instead of (10^8)?

\begin{figure}[h]
    \centering
    \pgfplotstableread[col sep=comma,]{data/test.csv}\datatable
    \begin{tikzpicture}
        \color{darkgrey}
        \begin{axis}[
            ybar,
            bar width=.3cm,
            width=\textwidth,
            height=5cm,
            ymin=0,
            xtick=data,
            xticklabels from table={\datatable}{Month},
            x tick label style={font=\normalsize, rotate=45, anchor=east},
            ylabel={Sales}]
            \addplot [fill=red] table [x expr=\coordindex, y={NumSales}]{\datatable};
        \end{axis}
    \end{tikzpicture}
    \caption{Total number of sales per month}
    \label{fig:total_sales}
\end{figure}

output plot

1
  • 1
    Please make your code fragment as compilable small document, which also contain table with data presented by diagram.
    – Zarko
    Commented Mar 16, 2020 at 0:43

1 Answer 1

4

You can use

scaled y ticks=manual:{millions}{\pgfmathparse{#1/10^6}},

This is a full compilable example.

\documentclass{article}
\begin{filecontents}{test.csv}
Month,NumSales
1,123456789
2,145678923
\end{filecontents}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\definecolor{darkgrey}{RGB}{80,80,80}
\begin{document}
\begin{figure}[h]
    \centering
    \pgfplotstableread[col sep=comma,]{test.csv}\datatable
    \begin{tikzpicture}
        \color{darkgrey}
        \begin{axis}[
            ybar,
            scaled y ticks=manual:{millions}{\pgfmathparse{#1/10^6}},
            bar width=.3cm,
            width=\textwidth,
            height=5cm,
            ymin=0,
            xtick=data,
            xticklabels from table={\datatable}{Month},
            x tick label style={font=\normalsize, rotate=45, anchor=east},
            ylabel={Sales}]
            \addplot [fill=red] table [x expr=\coordindex, y={NumSales}]{\datatable};
        \end{axis}
    \end{tikzpicture}
    \caption{Total number of sales per month}
    \label{fig:total_sales}
\end{figure}
\end{document}

enter image description here

As you can see, in order to give an example I had to make up some date (probably not the one you are using), to define a color (not the one you are using, perhaps it is somewhere but how would I know), and to complete the code (\documentclass etc.). For the future, I'd kindly like to ask you to provide a complete example (see also Zarko's comment).

3
  • Thanks for the answer! This will work for my current data, but would it automatically change to a different unit if new data were only in thousands for example?
    – LEJ
    Commented Mar 16, 2020 at 1:19
  • 1
    @LEJ no, it wouldn't. One may potentially cook up something that does that, but in order to do so, one would need clear prescriptions.The units library achieves something of that sort, maybe you want to have a look at section 5.14 Units in Labels of the pgfplots manual v1.16.
    – user194703
    Commented Mar 16, 2020 at 1:23
  • Thanks so much! I will take a look at that package for future reference :)
    – LEJ
    Commented Mar 16, 2020 at 1:28

You must log in to answer this question.

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