2

I want to plot data from file using TikZ, and I successfully do. However, I want to plot every value for the x-axis along the x-axis and remove the comma that appears for each number.

My current output:

enter image description here

Using the following code:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepackage{filecontents}


\begin{filecontents*}{kvinner.data}
anno,andel
1998,8.70
1999,22.73
2000,10.71
2001,26.32
2002,16.33
2003,23.53
2004,20.69
2005,22.08
2006,21.51
2007,26.67
2008,24.10
2009,22.33
2010,19.64
2011,21.05
2012,21.19
2013,20.44
2014,19.69
2015,21.58
2016,25.00
2017,27.38
2018,30.14
\end{filecontents*}


\begin{document}

\begin{tikzpicture}

\begin{axis}
\addplot table [x=anno, y=andel, col sep=comma] {kvinner.data};
\end{axis}

\end{tikzpicture}

\end{document}

The x-axis contains years, and the y-axis a precentage. How may I plot year value without the comma, and plot every year value along the x-axis?

4

A bit cramped, but using the following preamble you can do it:

\begin{axis}[
        xtick = {1998, 1999, ..., 2018},
        x tick label style={
            font=\small,
            rotate=90,
            /pgf/number format/.cd,%
            scaled x ticks = false,
            set thousands separator={},
            fixed
        }]

enter image description here

Wouldn't a bar plot be more appropriate for this data?

  • Thank you! Yes, you're right, it would be more appropriate with a bar plot. I'll see what I end up with. Anyhow, this is good knowledge. :) – matkapluku Jan 31 at 10:27
4

You can directly edit the number format of pgfplot options as in

\begin{axis}[x tick label style={/pgf/number format/.cd,%
        scaled x ticks = false,
        set thousands separator={},
        fixed}]
    ...
    \end{axis}

MWE:

\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepackage{filecontents}


\begin{filecontents*}{kvinner.data}
    anno,andel
    1998,8.70
    1999,22.73
    2000,10.71
    2001,26.32
    2002,16.33
    2003,23.53
    2004,20.69
    2005,22.08
    2006,21.51
    2007,26.67
    2008,24.10
    2009,22.33
    2010,19.64
    2011,21.05
    2012,21.19
    2013,20.44
    2014,19.69
    2015,21.58
    2016,25.00
    2017,27.38
    2018,30.14
\end{filecontents*}


\begin{document}

    \begin{tikzpicture}

    \begin{axis}[x tick label style={/pgf/number format/.cd,%
        scaled x ticks = false,
        set thousands separator={},
        fixed}]
    \addplot table [x=anno, y=andel, col sep=comma] {kvinner.data};
    \end{axis}

    \end{tikzpicture}

\end{document}

to get:

enter image description here

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.