1

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.enter image description here

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


\begin{document}
 \pgfplotstableread{
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!
      ]
      \addplot table [y index=1] {\datatable};
      \addplot table [y index=2] {\datatable};
      \addplot table [y index=3] {\datatable};
      \addplot table [y index=4] {\datatable};
      \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!
      ]
      \addplot table [y index=1] {\datatable};
      \addplot table [y index=2] {\datatable};
      \addplot table [y index=3] {\datatable};
      \addplot table [y index=4] {\datatable};
      \legend{Factor1,Factor2,Factor3,Factor4}
    \end{axis}
  \end{tikzpicture}
\end{document}
5

1 Answer 1

1

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,
compat=newest,
height=5cm}


\begin{document}
 \pgfplotstableread{
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
      ]
      \addplot table [y index=1] {\datatable};
      \addplot table [y index=2] {\datatable};
      \addplot table [y index=3] {\datatable};
      \addplot table [y index=4] {\datatable};
    \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}
      ]
      \addplot table [y index=1] {\datatable};
      \addplot table [y index=2] {\datatable};
      \addplot table [y index=3] {\datatable};
      \addplot table [y index=4] {\datatable};
      \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,
compat=newest,
height=5cm}


\begin{document}
 \pgfplotstableread{
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}
4
  • 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. Dec 9, 2018 at 21:57
  • 1
    @thesilencer Ehm, you kind of have the answer to that in your code already: height=<some length>. Dec 9, 2018 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. Dec 9, 2018 at 22:12
  • 1
    @thesilencer Don't post additional code in answers, answers are for answers. Remove y=.., keep height=.. Dec 9, 2018 at 22:18

You must log in to answer this question.

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