# label text into x-axis instead of numbers pgfplotstable

Does anyone know how can i enter text into the x-axis ? It seems like only numbers are allowed as soon as i wanted to enter alphabets it will be an error.

This template was adapted from here.

    \documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{   width=5cm,
height=5cm}

\begin{document}
Year    OneCol  SecCol  ThirdCol ForthCol
2005    10     70      -10     30
2006    -40     60      -15     90
2007    -20     60      -15     60
}\datatable

\begin{tikzpicture}
\begin{axis}[
x tick label style={/pgf/number format/1000 sep=},
ybar stacked,
bar width=1em,
ymin=-100,
ymax=200,
legend style={
at={(0.5,-0.1)},
anchor=north,
draw=none },
restrict y to domain*=:0, % negative values
y=1pt,  % important workaround!
]
\legend{Factor1,Factor2,Factor3,Factor4}
\end{axis}
\begin{axis}[
x tick label style={/pgf/number format/1000 sep=},
ybar stacked,
bar width=1em,
ymin=-100,
ymax=200,
legend style={
at={(0.5,-0.1)},
anchor=north,
draw=none },
restrict y to domain*=0:, % positive values
y=1pt,  % important workaround!
]
\legend{Factor1,Factor2,Factor3,Factor4}
\end{axis}
\end{tikzpicture}
\end{document}

• Look at symbolic x coords, or use xticklabels={A, B, C}. – Torbjørn T. Dec 9 '18 at 19:30
• – Torbjørn T. Dec 9 '18 at 19:38
• Final comment: xticklabels from table could be another option: tex.stackexchange.com/questions/63335/… – Torbjørn T. Dec 9 '18 at 19:39
• Thanks for your comments @TorbjørnT. but I've tried them before and none of them worked. – thesilencer Dec 9 '18 at 19:49
• I'm sure all of them work, but I see you might need xtick=data as well. – Torbjørn T. Dec 9 '18 at 19:56

Add hide axis to the first axis environment, and xtick=data, xticklabels={GWP,ODP,POCP} to the second axis environment.

To control the size of the axis, it's easiest to use just width=<some length>, height=<some length>.

By the way, you could define a style for the options that are common to the two axes, see example below.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}

\pgfplotsset{   width=5cm,
height=5cm}

\begin{document}
Year    OneCol  SecCol  ThirdCol ForthCol
2005    10     70      -10     30
2006    -40     60      -15     90
2007    -20     60      -15     60
}\datatable

\begin{tikzpicture}
\pgfplotsset{
PlotAxis/.style={ % common settings for the two axes
ybar stacked,
% enlarge x limits={abs=0.5}, % you may want this, try and see
bar width=1em,
ymin=-100,
ymax=200,
width=5cm,
height=10cm
}
}
\begin{axis}[
PlotAxis,
restrict y to domain*=:0, % negative values
hide axis % <-- added, you really only need to draw the axis and labels once
]
\end{axis}
\begin{axis}[
PlotAxis,
legend style={
at={(0.5,-0.1)},
anchor=north,
draw=none
},
restrict y to domain*=0:, % positive values
xtick=data,
xticklabels={GWP,ODP,POCP}
]
\legend{Factor1,Factor2,Factor3,Factor4}
\end{axis}
\end{tikzpicture}
\end{document}


You can actually make do with a single axis:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}

\pgfplotsset{   width=5cm,
height=5cm}

\begin{document}
Year    OneCol  SecCol  ThirdCol ForthCol
2005    10     70      -10     30
2006    -40     60      -15     90
2007    -20     60      -15     60
}\datatable

\begin{tikzpicture}
\begin{axis}[
ybar stacked,
% enlarge x limits={abs=0.5}, % you may want this, try and see
bar width=1em,
ymin=-100,
ymax=200,
width=5cm,
height=12cm,
legend style={
at={(0.5,-0.1)},
anchor=north,
draw=none
},
xtick=data,
xticklabels={GWP,ODP,POCP}
]

\pgfplotsinvokeforeach{1,...,4}{
\addplot +[restrict y to domain*=-Inf:0] table [y index=#1] {\datatable};
}
\pgfplotsset{cycle list shift=-4}
\pgfplotsinvokeforeach{1,...,4}{
\addplot +[restrict y to domain*=0:Inf] table [y index=#1] {\datatable};
}
\legend{Factor1,Factor2,Factor3,Factor4}
\end{axis}
\end{tikzpicture}
\end{document}

• Thanks i managed to do it now. Do you also know whether how can i extend the height of the chart? I tried y=<value>cm, but the command is just being ignored and nothing's happened. – thesilencer Dec 9 '18 at 21:57
• @thesilencer Ehm, you kind of have the answer to that in your code already: height=<some length>. – Torbjørn T. Dec 9 '18 at 21:58
• Yes it is already in the code, but somehow the command is being ignored. Do you mind helping me ? I have posted my codes as an answer. – thesilencer Dec 9 '18 at 22:12
• @thesilencer Don't post additional code in answers, answers are for answers. Remove y=.., keep height=.. – Torbjørn T. Dec 9 '18 at 22:18