条形图 - 单独更改每个条形的表面颜色

条形图 - 单独更改每个条形的表面颜色

我正在使用Matlab命令bar来创建条形图。我正在使用它matlab2tikz来转换它。我想将每个条形的表面颜色分别更改为我选择的 rgb 颜色。Matlab我可以使用此代码来实现这一点:

b = bar(x,y,'FaceColor','flat') b.CData(2,:) = [1 0 0]; b.CData(3,:) = [0 1 0]; b.CData(4,:) = [0 0 1]; b.CData(5,:) = [0 0 0]; b.CData(6,:) = [1 0 1];

然而,这会导致我的 所有条都透明texfile,如下所示:

% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
%  http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}

\begin{axis}[%
width=4.729in,
height=2.325in,
at={(0.793in,0.498in)},
scale only axis,
bar shift auto,
xmin=0,
xmax=7,
xtick={1,2,3,4,5,6},
xticklabels={{},{t = 3,0},{t = 3,5 s},{t = 4,0 s},{t = 5,0 s},{t \( \approx \) 30 s}},
xlabel style={font=\color{white!15!black}},
xlabel={Messreihennummer},
ymin=0,
ymax=160,
ytick={  0,  20,  40,  60,  80, 100, 120, 140, 160},
yminorticks=true,
ylabel style={font=\color{white!15!black}},
ylabel={Kan\"ulenauszugskraft in N},
axis background/.style={fill=white},
axis x line*=bottom,
axis y line*=left,
xmajorgrids,
xminorgrids,
ymajorgrids,
yminorgrids,
legend style={legend cell align=left, align=left, draw=white!15!black},
legend style={},
]
\addplot[ybar, bar width=0.8, fill=mycolor1, draw=black, area legend] table[row sep=crcr] {%
1   0\\
2   49.2731776918684\\
3   55.5745267232259\\
4   60.3699144999186\\
5   91.1499318440755\\
6   150.601191206667\\
};
\addplot[forget plot, color=white!15!black] table[row sep=crcr] {%
0   0\\
7   0\\
};
\addlegendentry{Probe 1}

\addplot [color=black, line width=2.0pt, draw=none]
 plot [error bars/.cd, y dir = both, y explicit]
 table[row sep=crcr, y error plus index=2, y error minus index=3]{%
1   0   0   0\\
2   49.2731776918684    19.964130020244 19.964130020244\\
3   55.5745267232259    15.9911247772533    15.9911247772533\\
4   60.3699144999186    7.89742116804573    7.89742116804573\\
5   91.1499318440755    13.3824237377185    13.3824237377185\\
6   150.601191206667    2.13783749065102    2.13783749065102\\
};
\addlegendentry{Probe 2}

\end{axis}
\end{tikzpicture}%

下面这行代码用来创建条形图:

\addplot[ybar, bar width=0.8, fill=mycolor1, draw=black, area legend] table[row sep=crcr] {%

使用最上面一行的 mycolor1:

\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%

这给出了以下结果:

在此处输入图片描述

我想调整 texfile,以便获得我想要的脸部颜色。这可以通过使用不同的 Matlab 代码或通过 Matlab 调整 texfile 来实现,如下所示:

    matlab2tikz('Uebersicht_Nadelauszugskraefte.tikz',...
        'extraaxisoptions',['legend style={legend columns=5},',...
        'legend style={font=\tiny},'])

非常感谢你的帮助!

答案1

直接使用 TikZ (而不是mathlab2tikz) 怎么样?

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[y=.5mm,x=1.5cm]
\draw[gray!50,local bounding box=L] (0,0) grid[xstep=1.5cm,ystep=20] (7,160);
\foreach \i/\ione/\itwo/\itext in
{1/0/0/,
2/49.2731776918684/19.964130020244/t=3.0,
3/55.5745267232259/15.9911247772533/t=3.5,
4/60.3699144999186/7.89742116804573/t=4.0,
5/91.1499318440755/13.3824237377185/t=4.5,
6/150.601191206667/2.13783749065102/t\approx 30
}{
\draw[blue!50,line width=10mm] 
(\i,0) node[below=-5mm,black]{$\itext$}--+(90:\ione) coordinate (tmp);
\draw[red,line width=10mm] (tmp)--+(90:\itwo);
}
\foreach \i in {0,...,8}{
\pgfmathsetmacro{\j}{int(20*\i)}    
\path (0,\j) node[left]{$\j$};
}

\draw (0,0) rectangle (7,160);
\path
(L.west)+(180:1) node[rotate=90]{Kan\"ulenauszugskraft in $N$}
(L.south)+(-90:18) node{Messreihennummer}
;

% legends (as a matrix node)
\path (2,130) node[matrix,draw,fill=white]{
\draw[line width=2mm,blue!50] (0,0)--+(0:.5);&\node{Probe 1};\\
\draw[line width=2mm,red] (0,0)--+(0:.5);&\node{Probe 2};\\
};
\end{tikzpicture}
\end{document}

相关内容