带有按索引排序的表列刻度标签的条形图?

带有按索引排序的表列刻度标签的条形图?

yticklabels from table如果我想通过索引定义列,该如何使用?

根据文档yticklabels from tableFurthermore, it can be [index] <integer> in which case <integer> is a column index.但由于某种原因,我无法让它工作。我在这里遗漏了什么?

下面是一个在 Overleaf 上出现错误的简单示例:

Package pgfplots Error: Sorry, could not retrieve column '0' from table '<inline_table>'. Please check spelling (or introduce name aliases).

我想要从第一列获取 4 个带标签的水平条,从第二列获取长度 - 如果没有解决方法,则第三列是条的顺序。

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
    compat=1.15,
}

\begin{document}
\pgfplotstableread{
Mangos 200 4
Bananas 123 3
Apples 50 2
Oranges 8 1
}\loadedtable;

\begin{tikzpicture}
    \begin{axis}[
        xbar,
        ytick=data,
        xmin=0,
        yticklabels from table={\loadedtable}{0}
    ]
        \addplot table [x index={1}, y index={2}] {\loadedtable};
    \end{axis}
\end{tikzpicture}

\end{document}

答案1

手册上说

Furthermore, it can be [index]⟨integer⟩ in which case ⟨integer⟩ is a column index.

确实有效,也就是说你忘记了[index]

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
    compat=1.15,
}

\begin{document}
\pgfplotstableread{
Mangos 200 4
Bananas 123 3
Apples 50 2
Oranges 8 1
}\loadedtable

\begin{tikzpicture}
    \begin{axis}[
        xbar,
        ytick=data,
        xmin=0,
        yticklabels from table={\loadedtable}{[index] 0}
    ]
        \addplot table [x index={1}, y index={2}] {\loadedtable};
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

( 之后不需要分号\pgfplotstableread。)

相关内容