2

I have two tables that hold different values for the same x values. To compare them, I want to plot both of them in the same plot but with different xlabels.

The tables should remain the same, i.e. I do not want to change the x values in the tables because they are also used for other plots.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            ybar, 
            xtick=data]
        \addplot table {
            1 1
        };
        \addplot table {
            1 2
        };
    \end{axis}
\end{tikzpicture}
\end{document}

This code produces the following image: current-state

What I want however, is something like this: enter image description here

I thought, there might be an easy option like addplot[x label=A] but so far I haven't found anything. Approaches I've tried:

  • xticklabels={A, B} - since the x values are the same, only the first label is printed. We obviously need something where we can tell pgfplots that the x values are not the same.
  • xticklabel={\pgfmathparse{\labelnames[Mod(\tick,2)]}\pgfmathresult} where \def\labelnames{{"A", "B"}} - same problem, the tick does not change.
  • symbolic x coords={A, B} - does not work since the x values are not named A and B.

3 Answers 3

2

Like this?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,width=7cm}     %% better add this
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
            ybar=-7pt,                  %% adjusted to make the bar be at the center of tick
            bar width=7pt,              %% adjusted to make the bar be at the center of tick
            enlargelimits=0.15,         %% my habit
            ylabel={},                  %% Add yours
            symbolic x coords={A,B},    %% Coordinates
            xtick={A,B},                %% ticks
        ]
    \addplot coordinates {(A,1)};
    \addplot coordinates {(B,2)};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3
  • Yes, that's what I want but without modifying the coordinates since I also use the tables for different plots. So I want them to remain 1 2 and 1 3 and not change them to A 2 and B 3.
    – mschrimpf
    Commented Apr 2, 2014 at 7:13
  • @Mash I am afraid that will take lot of tweaking. I am on the run now. Somebody may pop up soon.
    – user11232
    Commented Apr 2, 2014 at 8:36
  • I'm still hoping for a simple option that tells pgfplots to interpret the x-values differently. I could also duplicate and rearrange the files I'm reading from but I'm afraid that this could lead to inconsistency.
    – mschrimpf
    Commented Apr 2, 2014 at 9:43
1

How about adding something to the x value using x expr? This way, the data remains unchanged as you want, but the bars are offset and can have their own tick label.

Solution 1:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ybar,
    ybar=-7pt, %% Harish's answer.
    bar width=7pt, %% Harish's answer.
    xtick={1,2,3},
  ]

    \addplot table[x expr=\thisrowno{0}+0] {% Add 0 to x.
      1 1
    };
    \addplot table[x expr=\thisrowno{0}+1] {% Add 1 to x.
      1 2
    };
    \addplot table[x expr=\thisrowno{0}+2] {% Add 2 to x.
      1 1
    };

  \end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

You can immediately see that there's a pattern in the use of \addplot. Perhaps you can generalise for data in multiple columns using something like:

Solution 2:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ybar,
    xtick={1,...,6},
    ybar=-7pt, %% Harish's answer.
    bar width=7pt, %% Harish's answer.
  ]

    \pgfplotsinvokeforeach{1,...,6}{%
      \addplot table[row sep=crcr,x expr=\thisrowno{0}-1+#1, y index=#1] {
        1 1 2 3 3 2 1\\
      };
    }

  \end{axis}
\end{tikzpicture}
\end{document}

Output:

enter image description here

3
  • That sounds good, but it would be even better if the numbers could be text labels - is something like that possible?
    – mschrimpf
    Commented Apr 3, 2014 at 2:31
  • 1
    You can set the x tick labels using xticklabels={a,b,c} in the axis environment.
    – sudosensei
    Commented Apr 3, 2014 at 9:59
  • Yes, of course! That works. I combined it into a complete solution.
    – mschrimpf
    Commented Apr 4, 2014 at 2:42
1

For the sake of a complete answer to the question, I combined the the answers of sudosensei and Harish Kumar:

\documentclass{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    ybar=-7pt, %% Harish's answer
    bar width=7pt, %% Harish's answer
    xtick={1, 2}, %% sudosensei's answer
    xticklabels={A, B}, %% sudosensei's comment
  ]
    \addplot table[x expr=\thisrowno{0}+0] {% Add 0 to x.
      1 1
    };
    \addplot table[x expr=\thisrowno{0}+1] {% Add 1 to x.
      1 2
    };
  \end{axis}
\end{tikzpicture}
\end{document}

This leads to the desired result: Result

You must log in to answer this question.

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