我想为分组条形图定义不同的颜色,以下代码报错:
\documentclass[varwidth,border=7]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\pgfplotstableread[col sep=comma]{
key,value1,value2
AAA,36.4,40
BBB,92.5,90
CCC,92.6,80
DDD,92.7,50
EEE,98.2,20
FFF,98.3,30
}\mydata
\pgfplotstablegetrowsof{\mydata}
\pgfmathsetmacro{\numrows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\mydata}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval-1}
\newcommand{\colours}{{"00FF00","0000FF","4f81bd","c0504d","9bbb59","8064a2","4bacc6","f79646","0000ff"}}
\begin{document}
\begin{tikzpicture}[font=\tiny,
brace/.style={decorate, decoration={brace,amplitude=5pt,raise=1pt}},
]
\begin{axis}[
name=merging,
xmin = -.8,xmax=\numrows+.5,
ymin = 0,ymax=180,
xtick=data,
xticklabels from table={\mydata}{key},
xlabel = {keypoints},
ytick={},
ylabel = {Timestamp(secs)},
ybar=0,
x tick label style={rotate=40,anchor=east},
xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=north},
ylabel style = {at={(axis description cs:-0.05,.5)},rotate=0,anchor=south},
axis lines=left,
width=8cm,
height=6cm,
enlargelimits=0,
]
\foreach \i in {1,...,\numcols} {
\pgfmathsetmacro{\colour}{\colours[\i-1]}
\typeout{ \colour }
\definecolor{mycolor}{HTML}{\colour}
\addplot [draw=none,fill=mycolor] table [
x expr=\coordindex,
y index=\i,
col sep=comma,
] {\mydata};
\pgfplotstablegetcolumnnamebyindex{\i}\of{\mydata}\to{\colname}
\addlegendentryexpanded{\colname};
}
\end{axis}
\end{tikzpicture}
\end{document}
错误:
s been generated with the most recent feature set (\pgfplotsset{compat=1.18}).
00FF00
Missing character: There is no ; in font nullfont!
0000FF
Missing character: There is no ; in font nullfont!
! Package xcolor Error: Undefined color `mycolor'.
See the xcolor package documentation for explanation.
Type H <return> for immediate help.
...
l.54 \end{axis}
? x
No pages of output.
定义命令看起来不起作用!
答案1
应用@hperkrstiansen 评论 + 用评论修改,我得到了
\documentclass[varwidth,border=7]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\pgfplotstableread[col sep=comma]{
key,value1,value2
AAA,36.4,40
BBB,92.5,90
CCC,92.6,80
DDD,92.7,50
EEE,98.2,20
FFF,98.3,30
}\mydata
\def\mycolors{{"color1","color2"}}
\definecolor{color1}{RGB}{36,00,133}
\definecolor{color2}{RGB}{17,00,65}
\pgfplotstablegetrowsof{\mydata}
\pgfmathsetmacro{\numrows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\mydata}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval-1}
\begin{document}
\begin{tikzpicture}[font=\tiny,
brace/.style={decorate, decoration={brace,amplitude=5pt,raise=1pt}},
]
\begin{axis}[
name=merging,
xmin = -.8,xmax=\numrows+.5,
ymin = 0,ymax=180,
xtick=data,
xticklabels from table={\mydata}{key},
xlabel = {keypoints},
ytick={},
ylabel = {Timestamp(secs)},
ybar=0,
x tick label style={rotate=40,anchor=east},
xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=north},
ylabel style = {at={(axis description cs:-0.05,.5)},rotate=0,anchor=south},
axis lines=left,
width=8cm,
height=6cm,
enlargelimits=0,
bar width=0.2,
]
\pgfplotsinvokeforeach{1,...,\numcols}{
\pgfplotstablegetcolumnnamebyindex{#1-1}\of{\mydata}\to{\colname}
\pgfmathparse{\mycolors[#1-1]}
\edef\tempcolor{\pgfmathresult}
\addplot+[ybar,fill=\tempcolor] table [
x expr=\coordindex,
y index=#1,
col sep=comma,
] {\mydata};
\addlegendentryexpanded{\colname};
}
\end{axis}
\end{tikzpicture}
\end{document}