1

I'd like to use the, imho, most common value alignment of x- and y-values in an external file and then produce an xbar with it. It shall look like the examples here (the better example because there are two values for each ytick) or here. Is that possible?

I'd rather not enter the data for the coordinates myself.

The MWE is based upon this post and does not work.

MWE

\documentclass[
a4paper
]{scrartcl}

\usepackage{
    lmodern,
    amsmath
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{
    tikz,
    pgfplots
    }

\begin{filecontents}{data.csv}
Subjects;Min;Max
Test01;0.5;7
Test02;-2;4
Test03;-1;6
Test04;0;3
Test05;2;4
Test06;1;7
\end{filecontents}

\listfiles

\begin{document}
Test.
\begin{center}
\centering
\begin{tikzpicture}
\begin{axis}[
%
xbar,
xlabel={aaaa values},
ylabel={Subjects},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
%yticklabels from table={data.csv}{Subjects},
%symbolic y coords={Test01,Test02,Test03,Test04,Test05,Test06}
]
\addplot table [x index=1, y index=0, col sep=semicolon] {data.csv};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

MWE 2 for aligning bars & labels

This MWE is there to show that the alignment of the x-labels and bars is off.

\documentclass[
a4paper
]{scrartcl}

\usepackage{
    lmodern,
    amsmath
}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{
    tikz,
    pgfplots
    }

\begin{filecontents}{data.csv}
Subjects;Min;Max
Test01;0.5;7
Test02;-2;4
Test03;-1;6
Test04;0;3
Test05;2;4
Test06;1;7
\end{filecontents}

\pgfplotstableread[col sep=semicolon]{
Subjects;Min;Max
Test01;0.5;7
Test02;-2;4
Test03;-1;6
Test04;0;3
Test05;2;4
Test06;1;7
}\tableabcdef

\begin{document}
Test.
\begin{center}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar,
xlabel={aaaa values},
xmin=0,
%ytick=data,
nodes near coords, nodes near coords align={horizontal},
yticklabels from table={\tableabcdef}{Subjects},
]
%\addplot table [meta=Subjects, x=Min] {\tableabcdef};
\addplot table [meta=Subjects, x=Max] {\tableabcdef};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

Picture for MWE 2

enter image description here

henry
  • 6,594

1 Answers1

1

Two methods are utilized. The first method uses \pgfplotstableread which needs pgfplotstable, the second method uses filecontents environment

Update: About the same time as the first proposal was out, the OP updated a different sets of data and incorporated Jake's suggestions. Here is the updated output.

enter image description here

Updated Code

\documentclass[
a4paper
]{scrartcl}

\usepackage{
    lmodern,
    amsmath
}
\usepackage{pgfplotstable}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{
    tikz,
    pgfplots
    }

\begin{filecontents}{data.csv}
Subjects;Min;Max
Test01;0.5;7
Test02;-2;4
Test03;-1;6
Test04;0;3
Test05;2;4
Test06;1;7
\end{filecontents}

\pgfplotstableread[col sep=semicolon]{
Subjects;Min;Max
Test01;0.5;7
Test02;-2;4
Test03;-1;6
Test04;0;3
Test05;2;4
Test06;1;7
}\tableabcdef

\begin{document}
Test.
\begin{center}
\centering
\begin{tikzpicture}
\begin{axis}[
xbar,
xlabel={aaaa values},
xmin=-4,
ytick=data,
nodes near coords, nodes near coords align={horizontal},
yticklabels from table={\tableabcdef}{Subjects},
]
%\addplot table [meta=Subjects, x=Min] {\tableabcdef};
\foreach \i in {Min,Max}{
\addplot table [meta=Subjects, y expr=\coordindex, x=\i] {\tableabcdef};}
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

Code

\documentclass[a4paper]{scrartcl}

\usepackage{
    lmodern,
    amsmath
}
\usepackage{pgfplotstable}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{
    tikz,
    pgfplots
    }


\begin{filecontents}{data.csv}
Subjects,Min,Max
TestA,0.5,7
TestB,-2,4
WordA,-1,6
WordB,0,3
NameA,5,4
NameB,1,7
\end{filecontents}

\pgfplotstableread[col sep=comma]{
Subjects,Min,Max
TestA,0.5,7
TestB,-2,4
WordA,-1,6
WordB,0,3
NameA,5,4
NameB,1,7
}\datatable

%\listfiles

\begin{document}
Test.
\begin{center}
\centering
\begin{tikzpicture}
\begin{axis}[%
xbar, 
xlabel={aaaa values},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
yticklabels from table={\datatable}{Subjects}
]
\addplot table [meta=Subjects, x=Min] {\datatable};
\end{axis}

\end{tikzpicture}
\end{center}
%

\begin{center}
\centering
\begin{tikzpicture}
\begin{axis}[%
xbar,
xlabel={aaaa values},
ytick=data,
yticklabels = {TestA,TestB,WordA,WordB,NameA,NameB},
nodes near coords, nodes near coords align={horizontal},
%yticklabels from table={data.csv}{index=0}
]
\addplot table [meta index =0, x index =1, col sep = comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}
Jesse
  • 29,686
  • Thank you for your effort. I updated my MWE in the meantime but I can follows your code of course. Is it possible to display both values (min and max) for a subject? Also, the y-labels are aligned by their x values which is quite irritating. I am looking for a solution there myself but maybe someone else knows better already. – henry May 17 '14 at 09:14
  • You can change xbar to xbar stacked and add one more line \addplot table [meta=Subjects, x=Max] {\datatable}; – Jesse May 17 '14 at 09:29
  • Oh, right. Didn't see the forest for the trees here. – henry May 17 '14 at 09:34
  • Unfortunately it still doesn't fully work out the way I intend it to do. I added a second MWE with picture at the bottom of the op. – henry May 17 '14 at 09:48
  • 1
    enable the ytick=data. It was marked out. – Jesse May 17 '14 at 09:54
  • And Jake provided the other relevant tidbit: 1. loading pgfplotstable 2. adding y expr=\coordindex as an option to the \addplot line. So I'll mark your post then. If you could edit those details in, it would be greatly appreciated. :) – henry May 17 '14 at 09:59
  • @henry -- added Jake's suggestion. – Jesse May 17 '14 at 10:18
  • Note that instead of using \pgfplotstableread you can instead use the key table/col sep=comma, h/t https://tex.stackexchange.com/a/578296/2066 – Jason Gross Jan 10 '21 at 14:38