我想制作堆积条形图的颜色(如这里)取决于数据(如这里) 和这里。
到目前为止,这是我所拥有的,但我不知道如何访问数据的“颜色”列以使特定行的条形图呈现该颜色。
\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.14}
\usepackage[utf8]{inputenc}
% read in table
\pgfplotstableread[col sep=comma]{
author,startyear,endyear,color
abc,1530,1570,red
def,1560,1570,green
ghi,1560,1660,blue
}\loadedtable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
nodes near coords xbar stacked configuration/.style={},
nodes near coords style={font=\footnotesize},
xbar stacked,
xmin=1500,
bar width=0.2cm,
axis lines=left,
width=\textwidth, height=7cm,
enlarge y limits={abs=0.5},
ytick=\empty,
]
\addplot [draw=none, forget plot] table [col sep=comma,x=startyear, y expr=-\coordindex]{\loadedtable};
\addplot +[
nodes near coords,
nodes near coords align={anchor=west},
point meta=explicit symbolic] table[col sep=comma,x expr=\thisrow{endyear}-\thisrow{startyear}, y expr=-\coordindex,meta=author]{\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
编辑:抱歉,之前的回答有问题。我对堆栈条形图函数的理解完全错误。我编辑了答案,让它正常工作。
这不是完美的解决方案,但最终还是奏效了。现在图表是从单个.txt
文件绘制的。这些\addplot
命令仅用于绘制轴和定义条形图位置(您不能为不同的条形图添加图例)。条形图实际上是使用\draw
之后的命令绘制的\addplot
。使用\foreach
和datatool
包命令绘制一条粗线,用不同的颜色来表示条形图。文件中有任意数量的行.txt
,为每个条形图指定颜色,使用 定义垂直位置y
。
\begin{filecontents*}{a.txt}
author,startyear,endyear,y,color
abc1,1520,1570,6,red
def1,1540,1590,5,green
abc2,1550,1575,4,yellow
def2,1560,1570,3,green
ghi1,1560,1660,2,blue
ghi2,1620,1660,1,purple
jkl1,1590,1650,0,orange
\end{filecontents*}
\documentclass{article}
\usepackage{datatool}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage[utf8]{inputenc}
\DTLloaddb{tablea}{a.txt}
\foreach \i in {1,...,\DTLrowcount{tablea}}{
\edef\tempi{\noexpand\DTLassign{tablea}{\i}{\csname xst\i\endcsname=startyear,\csname xed\i\endcsname=endyear,\csname yp\i\endcsname=y,\csname col\i\endcsname=color}}\tempi}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
nodes near coords xbar stacked configuration/.style={},
nodes near coords style={font=\footnotesize},
xmin=1500,
bar width=0.2cm,
xbar stacked,
stack negative=on previous,
axis lines=left,
width=\textwidth, height=7cm,
enlarge y limits={abs=0.5},
ytick=\empty,
]
\addplot [draw=none, forget plot] table [col sep=comma,x=startyear, y=y]{a.txt};
\addplot[
nodes near coords,draw=none,
nodes near coords align={anchor=west},
point meta=explicit symbolic] table [col sep=comma,x expr=\thisrow{endyear}-\thisrow{startyear}, y expr=\thisrow{y},meta index=0]{a.txt};
\foreach \i in {1,...,\DTLrowcount{tablea}}{
\edef\tempii{\noexpand\draw [line width=8pt,\csname col\i\endcsname] (axis cs:\csname xst\i\endcsname,\csname yp\i\endcsname) -- (axis cs:\csname xed\i\endcsname,\csname yp\i\endcsname);}\tempii}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
以下是如何在同一个文件中添加多个这样的图(来自@Tom):
\begin{filecontents*}{a.txt}
author,startyear,endyear,y,color
abc1,1520,1570,6,red
def1,1540,1590,5,green
abc2,1550,1575,4,yellow
def2,1560,1570,3,green
ghi1,1560,1660,2,blue
ghi2,1620,1660,1,purple
jkl1,1590,1650,0,orange
\end{filecontents*}
\begin{filecontents*}{b.txt}
author,startyear,endyear,y,color
jkl1,1590,1650,0,orange
\end{filecontents*}
\documentclass{article}
\usepackage{datatool}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage[utf8]{inputenc}
\DTLloaddb{tablea}{a.txt}
\foreach \i in {1,...,\DTLrowcount{tablea}}{
\edef\tempi{\noexpand\DTLassign{tablea}{\i}{\csname axst\i\endcsname=startyear,\csname axed\i\endcsname=endyear,\csname ayp\i\endcsname=y,\csname acol\i\endcsname=color}}\tempi}
\DTLloaddb{tableb}{b.txt}
\foreach \j in {1,...,\DTLrowcount{tableb}}{
\edef\tempj{\noexpand\DTLassign{tableb}{\j}{\csname bxst\j\endcsname=startyear,\csname bxed\j\endcsname=endyear,\csname byp\j\endcsname=y,\csname bcol\j\endcsname=color}}\tempj}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
nodes near coords xbar stacked configuration/.style={},
nodes near coords style={font=\footnotesize},
xmin=1500,
bar width=0.2cm,
xbar stacked,
stack negative=on previous,
axis lines=left,
width=\textwidth, height=7cm,
enlarge y limits={abs=0.5},
ytick=\empty,
]
\addplot [draw=none, forget plot] table [col sep=comma,x=startyear, y=y]{a.txt};
\addplot[
nodes near coords,draw=none,
nodes near coords align={anchor=west},
point meta=explicit symbolic] table [col sep=comma,x expr=\thisrow{endyear}-\thisrow{startyear}, y expr=\thisrow{y},meta index=0]{a.txt};
\foreach \i in {1,...,\DTLrowcount{tablea}}{
\edef\tempii{\noexpand\draw [line width=8pt,\csname acol\i\endcsname] (axis cs:\csname axst\i\endcsname,\csname ayp\i\endcsname) -- (axis cs:\csname axed\i\endcsname,\csname ayp\i\endcsname);}\tempii}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
nodes near coords xbar stacked configuration/.style={},
nodes near coords style={font=\footnotesize},
xmin=1500,
bar width=0.2cm,
xbar stacked,
stack negative=on previous,
axis lines=left,
width=\textwidth, height=7cm,
enlarge y limits={abs=0.5},
ytick=\empty,
]
\addplot [draw=none, forget plot] table [col sep=comma,x=startyear, y=y]{b.txt};
\addplot[
nodes near coords,draw=none,
nodes near coords align={anchor=west},
point meta=explicit symbolic] table [col sep=comma,x expr=\thisrow{endyear}-\thisrow{startyear}, y expr=\thisrow{y},meta index=0]{b.txt};
\foreach \j in {1,...,\DTLrowcount{tableb}}{
\edef\tempjj{\noexpand\draw [line width=8pt,\csname bcol\j\endcsname] (axis cs:\csname bxst\j\endcsname,\csname byp\j\endcsname) -- (axis cs:\csname bxed\j\endcsname,\csname byp\j\endcsname);}\tempjj}
\end{axis}
\end{tikzpicture}
\end{document}