从表值中选择条形图的颜色

从表值中选择条形图的颜色

我正在尝试根据行中单元格的值来选择堆积条形图的颜色。

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage[utf8]{inputenc}

\begin{document}

\pgfplotstableread[col sep=comma]{
label,startyear,endyear,color  
Washington, 1789, 1797, black!50
Adams, 1797, 1801, red!30
Jefferson, 1801, 1809, green!50
Madison, 1801, 1809, green!50
}\loadedtable
% sort by startyear
\pgfplotstablesort[sort key=startyear]\sortedtable\loadedtable

\begin{tikzpicture}
\begin{axis}[
  xbar stacked,
  xmin=1785,
  bar width=0.2cm,
  axis lines=left,
  width=\textwidth, height=4cm,
  enlarge y limits={abs=0.5},
  ytick=\empty,
]

\addplot [draw=none, forget plot] table [col sep=comma,x=startyear, y expr=-\coordindex]{\sortedtable};
\addplot +[
   nodes near coords,
   nodes near coords align={anchor=west},
   point meta=explicit symbolic, every node near coord/.append style={black}] table[col sep=comma,x expr=\thisrow{endyear}-\thisrow{startyear}, y expr=-\coordindex,meta=label]{\sortedtable};
\end{axis}
\end{tikzpicture}
\end{document}

但是我还没有找到让每一行都填充选定颜色的方法。

答案1

用不同的颜色绘制这些条形并不太难。这不是绘制条形,而是绘制矩形,当然看起来像条形。相应绘图标记的定义来自这里。(我不明白为什么需要对表格进行排序。)

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\usepackage[utf8]{inputenc}
\pgfkeys{/pgf/shapes/xbar/height/.initial=10cm,/pgf/shapes/xbar/width/.initial=6pt}
\pgfdeclareplotmark{xbar}{%
    \pgfpathrectangle{\pgfpoint{0pt}{-.5*\pgfkeysvalueof{/pgf/shapes/xbar/width}}{0pt}}{%
    \pgfpoint{-\pgfkeysvalueof{/pgf/shapes/xbar/height}}{\pgfkeysvalueof{/pgf/shapes/xbar/width}}}
    \pgfusepath{stroke,fill}}
\begin{document}

\pgfplotstableread[col sep=comma]{
label,startyear,endyear,color  
Washington, 1789, 1797, black!50
Adams, 1797, 1801, red!30
Jefferson, 1801, 1809, green!50
Madison, 1801, 1809, green!50
}\loadedtable
% sort by startyear
\pgfplotstablesort[sort key=startyear]\sortedtable\loadedtable

\begin{tikzpicture}
\begin{axis}[xmin=1785,xmax=1810,height=4cm,
  /pgf/shapes/xbar/width=0.2cm,
  axis lines=left,
  width=\textwidth, 
  enlarge y limits={abs=0.5},
  ytick=\empty,
  scatter/@pre marker code/.code={
  \pgfplotstablegetelem{\coordindex}{color}\of{\sortedtable}
  \edef\mycolor{\pgfplotsretval}
  \pgfplotstablegetelem{\coordindex}{startyear}\of{\sortedtable}
  \edef\startyear{\pgfplotsretval}
  \pgfplotstablegetelem{\coordindex}{endyear}\of{\sortedtable}
  \edef\endyear{\pgfplotsretval}
  \pgfmathsetmacro{\myheight}{(\endyear-\startyear)*\xunit}
  \scope[fill=\mycolor,/pgf/shapes/xbar/height=\myheight pt]},
]

\addplot [xbar stacked,draw=none, forget plot] table [col sep=comma,x=startyear, y expr=-\coordindex]{\sortedtable};
\path let \p1=($(1786,0)-(1785,0)$)    in \pgfextra{\xdef\xunit{\x1}}; % measure x unit
\addplot[only marks,scatter,mark=xbar,
   nodes near coords*,
   nodes near coords align={anchor=west},
   point meta=explicit symbolic, every node near coord/.append style={black}] 
 table[col sep=comma,y expr=-\coordindex,x expr=\thisrow{endyear},meta=label]{\sortedtable};

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

在此处输入图片描述

以下是添加图例的一种方法。

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\usepackage[utf8]{inputenc}
\pgfkeys{/pgf/shapes/xbar/height/.initial=10cm,/pgf/shapes/xbar/width/.initial=6pt}
\pgfdeclareplotmark{xbar}{%
    \pgfpathrectangle{\pgfpoint{0pt}{-.5*\pgfkeysvalueof{/pgf/shapes/xbar/width}}{0pt}}{%
    \pgfpoint{-\pgfkeysvalueof{/pgf/shapes/xbar/height}}{\pgfkeysvalueof{/pgf/shapes/xbar/width}}}
    \pgfusepath{stroke,fill}}
\begin{document}

\pgfplotstableread[col sep=comma]{
label,startyear,endyear,color  
Washington, 1789, 1797, black!50
Adams, 1797, 1801, red!30
Jefferson, 1801, 1809, green!50
Madison, 1801, 1809, green!50
}\loadedtable
% sort by startyear
\pgfplotstablesort[sort key=startyear]\sortedtable\loadedtable

\begin{tikzpicture}
\begin{axis}[xmin=1785,xmax=1810,height=4cm,
  /pgf/shapes/xbar/width=0.2cm,
  axis lines=left,
  width=\textwidth, 
  enlarge y limits={abs=0.5},
  ytick=\empty,
  scatter/@pre marker code/.code={
  \pgfplotstablegetelem{\coordindex}{color}\of{\sortedtable}
  \edef\mycolor{\pgfplotsretval}
  \pgfplotstablegetelem{\coordindex}{startyear}\of{\sortedtable}
  \edef\startyear{\pgfplotsretval}
  \pgfplotstablegetelem{\coordindex}{endyear}\of{\sortedtable}
  \edef\endyear{\pgfplotsretval}
  \pgfmathsetmacro{\myheight}{(\endyear-\startyear)*\xunit}
  \scope[fill=\mycolor,/pgf/shapes/xbar/height=\myheight pt]},
  legend style={at={(1,1.5)}}
]

\addplot [xbar stacked,draw=none, forget plot] table [col sep=comma,x=startyear, y expr=-\coordindex]{\sortedtable};
\path let \p1=($(1786,0)-(1785,0)$)    in \pgfextra{\xdef\xunit{\x1}}; % measure x unit
\addplot[only marks,scatter,mark=xbar,
   nodes near coords*,forget plot,
   nodes near coords align={anchor=west},
   point meta=explicit symbolic, every node near coord/.append style={black}] 
 table[col sep=comma,y expr=-\coordindex,x expr=\thisrow{endyear},meta=label]{\sortedtable};
 \pgfplotstablegetrowsof{\sortedtable} 
 \pgfmathtruncatemacro{\NumRows}{\pgfplotsretval-1} 
 \pgfplotsinvokeforeach{0,...,\NumRows}{
  \pgfplotstablegetelem{#1}{color}\of{\sortedtable}
  \edef\mycolor{\pgfplotsretval}
  \pgfplotstablegetelem{#1}{label}\of{\sortedtable}
  \edef\mylabel{\pgfplotsretval}
  \edef\temp{\noexpand\addlegendimage{area legend,fill=\mycolor}
  \noexpand\addlegendentry{\mylabel}}
  \temp
 }
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容