Here's how to add multiple of these plots in the same file (from @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}
foreachasa/red,b/green,c/red,d/...– Tom Jun 09 '22 at 02:31foreachmeans the table file name and bar colorname/color. – Tom Jun 09 '22 at 02:34foreach! What is the purpose of "wholetable.txt"? seems it's not used? – rrrrr Jun 09 '22 at 02:35DTLassigncommand in the preamble. I changed the code. – Tom Jun 09 '22 at 22:25a.txt, but I have issue when i have ana.txtand a differentb.txt(please see my posted answer below). Did I make a mistake? – rrrrr Jun 09 '22 at 22:44