如何使用 pgfplots 创建带有字符串标签的直方图?

如何使用 pgfplots 创建带有字符串标签的直方图?

亲爱的,我尝试生成直方图/条形图

结果.csv:

overlap,hit,organism
100,274,Unknowns
100,228,Coccomyxa subellipsoidea
100,11,Eukaryota
-500,153,Unknowns
-500,145,Coccomyxa subellipsoidea
-500,7,Eukaryota
-1000,118,Unknowns
-1000,108,Coccomyxa subellipsoidea
-1000,4,cellular organisms
-2000,74,Coccomyxa subellipsoide
-2000,72,Unknowns
-2000,4,Viridiplantae
-4000,51,Unknowns
-4000,42,Coccomyxa subellipsoidea
-4000,2,Eukaryota
-8000,26,Coccomyxa subellipsoidea
-8000,25,Unknowns
-8000,1,Eukaryota
-16000,14,Coccomyxa subellipsoidea
-16000,13,Unknowns
-16000,1,cellular organisms

如您所见,我们可以使用重叠将数据分为 7 个类别:

100、-500、-1000、-2000、-4000、-8000、-16000

\documentclass[table]{beamer}

\usepackage[american]{babel}
\usepackage{etex}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\pgfplotsset{
  compat        = newest,
  table/col sep = comma
}


\begin{document}

\begin{frame}
    \begin{tikzpicture}
        \pgfplotstableread{result.csv}\table
        \centering
        \begin{axis}[
            visualization depends on={value \thisrow{organism} \as \labela},
            visualization depends on={value \thisrow{hit} \as \labelb},
            ybar,
            height                = 6cm,
            width                 = 10cm,
            enlarge y limits      = false,
            axis lines*           = left,
            ymin                  = 0,
            ymax                  = 300,
            xmin                  = -17000,
            xmax                  = 600,
            legend style          = {
              at              = {(0.5,-0.2)},
              anchor          = north,
              legend columns  = -1
            },
            xtick                 = data,
            scaled y ticks        = false,
            y tick label style    = {/pgf/number format/fixed,/pgf/number format/1000 sep = \thinspace},
            xticklabels from table= \table{overlap},
             nodes near coords={\labela/\pgfmathprintnumber\labelb},
            every node near coord/.append style={ anchor=mid west, rotate=80 }
        ]
        \foreach \i in {1,2,...,21}{
          \addplot table[ x index=\i, y index=1 ] \table;
        }
      \end{axis}
  \end{tikzpicture}
\end{frame}

\end{document}

但我无法正确绘图,当前代码给出错误:

! Package PGF Math Error: Could not parse input 'Unknowns' as a floating point 
number, sorry. The unreadable part was near 'Unknowns'.

感谢您的帮助

答案1

要使用字符串作为条形图的标签,您可以通过在图表选项中point meta=explicit symbolic设置来使用并指定将哪一列用作每个图表的标签。meta=<column name>table

在这种情况下,我将重新组织数据表,按重叠部分对生物体和命中次数进行分组:

\documentclass{article}

\usepackage{pgfplots, xstring}

\pgfplotsset{compat=newest}
\usepackage{filecontents}

\begin{filecontents}{dataedited.csv}
overlap,hit1,organism1,hit2,organism2,hit3,organism3
100,274,Unknowns,228,Coccomyxa subellipsoidea,11,Eukaryota
-500,153,Unknowns,145,Coccomyxa subellipsoidea,7,Eukaryota
-1000,118,Unknowns,108,Coccomyxa subellipsoidea,4,cellular organisms
-2000,74,Coccomyxa subellipsoide,72,Unknowns,4,Viridiplantae
-4000,51,Unknowns,42,Coccomyxa subellipsoidea,2,Eukaryota
-8000,26,Coccomyxa subellipsoidea,25,Unknowns,1,Eukaryota
-16000,14,Coccomyxa subellipsoidea,13,Unknowns,1,cellular organisms
\end{filecontents}

\begin{document}
\begin{tikzpicture}
        \pgfplotstableread[col sep=comma]{dataedited.csv}\table
        \centering
        \begin{axis}[
            xbar, bar width=7pt,
            height                = 11cm,
            width                 = 10cm,
            enlarge x limits      = false,
            axis lines*           = left,
            legend style          = {
              at              = {(0.5,-0.2)},
              anchor          = north,
              legend columns  = -1
            },
            xmin = 0,
            ytick                 = data,
            scaled x ticks        = false,
            yticklabels from table= \table{overlap},
            point meta=explicit symbolic,
            nodes near coords={
                \IfStrEq{\pgfplotspointmeta}{Coccomyxa subellipsoidea}{\textcolor{red}{\pgfplotspointmeta}}{\pgfplotspointmeta}
            },
            every node near coord/.append style={anchor=mid west, yshift=-0.5ex, black!75}
        ]
          \addplot [fill=gray] table [col sep=comma, y expr=-\coordindex, x=hit3, meta=organism3] {dataedited.csv};
          \addplot [fill=gray!75] table [col sep=comma, y expr=-\coordindex, x=hit2, meta=organism2] {dataedited.csv};
          \addplot [fill=gray!50] table [col sep=comma, y expr=-\coordindex, x=hit1, meta=organism1] {dataedited.csv};
      \end{axis}
  \end{tikzpicture}
\end{document}

相关内容