我想使用外部文件中最常见的 x 和 y 值对齐方式,然后使用xbar
它生成一个。它应该看起来像示例这里(更好的例子,因为每个都有两个值ytick
)或这里。 那可能吗?
我不想自己输入坐标数据。
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 用于对齐条形图和标签
此 MWE 表示 x 标签和条的对齐不正确。
\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}
MWE 2 的图片
答案1
有两种方法。第一种方法使用\pgfplotstableread
需要pgfplotstable
,第二种方法使用filecontents
环境
更新:大约在第一份提案发布的同时,OP 更新了一组不同的数据并采纳了 Jake 的建议。以下是更新后的输出。
更新的代码
\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}
代码
\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}