我想要为数轴上所有不同的条形(范围从B_{1}
到)分配颜色。B_{15}
% !TeX program = lualatex
\documentclass[a4paper,14pt]{extarticle}
\usepackage{mathptmx}% http://ctan.org/pkg/mathptmx% is TIME NEW ROMANS
\usepackage{tgtermes} % times font
%\usepackage{wordlike}% Emulate MS Word
\usepackage{setspace}
% Double spacing
%\doublespacing
%% One and half spacing
\onehalfspacing
% the below reduce space between section header and text
\usepackage{titlesec}
\titlespacing{\section}{0pt}{\parskip}{\parskip}
\titlespacing{\subsection}{0pt}{\parskip}{\parskip}
\titlespacing{\subsubsection}{0pt}{\parskip}{\parskip}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{amssymb,amsfonts,amsmath,graphicx,autobreak}
\usepackage{changepage,mathtools,amsthm,tikz,blindtext}
\usepackage{inputenc,float,caption,subcaption,enumitem}
\usepackage[font=large,labelfont=bf,font=normalfont,labelsep=colon]{caption}
\usepackage[font=small,labelfont=bf,font=bf,labelsep=colon]{subcaption}
\usepackage{csquotes} %for single quoatation mark
\usepackage{tikz}
\begin{document}
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}
% draw horizontal line
\draw (0,0) -- (17,0);
% draw vertical lines
\foreach \x in {0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5}
\draw (\x cm,5pt) -- (\x cm,-0pt);
% draw nodes
\draw (0.5,0) node[above=5pt] {$ x_{1} $} node[above=5pt] {$\rm $};
\draw (1.5,0) node[above=5pt] {$ x_{2} $} node[above=5pt] {$\rm $};
\draw (2.5,0) node[above=5pt] {$ x_{3} $} node[above=5pt] {$\rm $};
\draw (3.5,0) node[above=5pt] {$ x_{4} $} node[above=5pt] {$\rm $};
\draw (4.5,0) node[above=3pt] {$ x_{5} $} node[above=5pt] {$\rm $};
\draw (5.5,0) node[above=5pt] {$ x_{6} $} node[above=5pt] {$\rm $};
\draw (6.5,0) node[above=3pt] {$ x_{7} $} node[above=5pt] {$\rm $};
\draw (7.5,0) node[above=5pt] {$ x_{8} $} node[above=5pt] {$\rm $};
\draw (8.5,0) node[above=3pt] {$ x_{9} $} node[above=5pt] {$\rm $};
\draw (9.5,0) node[above=3pt] {$ x_{10} $} node[above=5pt] {$\rm $};
\draw (10.5,0) node[above=3pt] {$ x_{l1} $} node[above=5pt] {$\rm $};
\draw (11.5,0) node[above=3pt] {$ x_{l2} $} node[above=5pt] {$\rm $};
\draw (12.5,0) node[above=3pt] {$ x_{l3} $} node[above=5pt] {$\rm $};
\draw (13.5,0) node[above=3pt] {$ x_{14} $} node[above=5pt] {$\rm $};
\draw (14.5,0) node[above=3pt] {$ x_{l5} $} node[above=5pt] {$\rm $};
\draw (15.5,0) node[above=3pt] {$ x_{16} $};
\draw (16.5,0) node[above=5pt] {$ x_{n} $} node[above=5pt] {$\rm $};
\draw [-] (0.5,-0.1) -- (0.5,-0.5);
\draw [-] (2.5,-0.1) -- (2.5,-0.5);
\draw [-] (0.5,-0.3) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{1} $} (2.5,-0.3); %1
\draw [-] (1.5,0.9) -- (1.5,0.5);
\draw [-] (3.5,0.9) -- (3.5,0.5);
\draw [-] (1.5,0.7) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{2} $} (3.5,0.7); %2
\draw [-] (2.5,-0.6) -- (2.5,-1.0);
\draw [-] (4.5,-0.6) -- (4.5,-1.0);
\draw [-] (2.5,-0.8) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{3} $} (4.5,-0.8); %3
\draw [-] (3.5,1.0) -- (3.5,1.4);
\draw [-] (5.5,1.0) -- (5.5,1.4);
\draw [-] (3.5,1.2) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{4} $} (5.5,1.2); %4
\draw [-] (4.5,-0.1) -- (4.5,-0.5);
\draw [-] (6.5,-0.1) -- (6.5,-0.5);
\draw [-] (4.5,-0.3) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{5} $} (6.5,-0.3); %5
\draw [-] (5.5,0.9) -- (5.5,0.5);
\draw [-] (7.5,0.9) -- (7.5,0.5);
\draw [-] (5.5,0.7) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{6} $} (7.5,0.7); %6
\draw [-] (6.5,-0.6) -- (6.5,-1.0);
\draw [-] (8.5,-0.6) -- (8.5,-1.0);
\draw [-] (6.5,-0.8) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{7} $} (8.5,-0.8); %7
\draw [-] (7.5,1.0) -- (7.5,1.4);
\draw [-] (9.5,1.0) -- (9.5,1.4);
\draw [-] (7.5,1.2) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{8} $} (9.5,1.2); %8
\draw [-] (8.5,-0.1) -- (8.5,-0.5);
\draw [-] (10.5,-0.1) -- (10.5,-0.5);
\draw [-] (8.5,-0.3) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{9} $} (10.5,-0.3); %9
\draw [-] (9.5,0.9) -- (9.5,0.5);
\draw [-] (11.5,0.9) -- (11.5,0.5);
\draw [-] (9.5,0.7) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{10} $} (11.5,0.7); %10
\draw [-] (10.5,-0.6) -- (10.5,-1.0);
\draw [-] (12.5,-0.6) -- (12.5,-1.0);
\draw [-] (10.5,-0.8) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{11} $} (12.5,-0.8); %11
\draw [-] (11.5,1.0) -- (11.5,1.4);
\draw [-] (13.5,1.0) -- (13.5,1.4);
\draw [-] (11.5,1.2) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{12} $} (13.5,1.2); %12
\draw [-] (12.5,-0.1) -- (12.5,-0.5);
\draw [-] (14.5,-0.1) -- (14.5,-0.5);
\draw [-] (12.5,-0.3) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{13} $} (14.5,-0.3); %13
\draw [-] (13.5,0.9) -- (13.5,0.5);
\draw [-] (15.5,0.9) -- (15.5,0.5);
\draw [-] (13.5,0.7) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{14} $} (15.5,0.7); %14
\draw [-] (14.5,-0.6) -- (14.5,-1.0);
\draw [-] (16.5,-0.6) -- (16.5,-1.0);
\draw [-] (14.5,-0.8) --node [midway,anchor=center,fill=white,inner sep=0.5pt] {$ B_{15} $} (16.5,-0.8); %15
\end{tikzpicture}
\caption{ Moving Block Bootstrap: Block size $= 2$, Overlapping $= 1$}
\label{fig1}
\end{center}
\end{figure}
\end{document}
我希望条形图的颜色与其他条形图的颜色B_{1}
不同。B_{1}
我还希望数轴本身($x_{1}, x_{2}, \dots, x_{n}$)的字体颜色统一,同时条形图($B_{1}, B_{2}, \dots, B_{15}$)的字体颜色也统一不同。
答案1
你的意思是这样的(颜色是随机的=概念证明):
\documentclass[border=3.14mm,tikz]{standalone}
\definecolor{color1}{RGB}{125,125,234}
\definecolor{color3}{RGB}{0,34,212}
\definecolor{color5}{RGB}{25,125,234}
\definecolor{color7}{RGB}{18,128,200}
\definecolor{color9}{RGB}{125,25,234}
\definecolor{color11}{RGB}{125,25,234}
\definecolor{color13}{RGB}{125,125,34}
\definecolor{color15}{RGB}{5,125,34}
\definecolor{color2}{RGB}{125,125,234}
\definecolor{color4}{RGB}{0,34,212}
\definecolor{color6}{RGB}{25,125,234}
\definecolor{color8}{RGB}{18,128,200}
\definecolor{color10}{RGB}{125,25,234}
\definecolor{color12}{RGB}{125,25,234}
\definecolor{color14}{RGB}{125,125,34}
\begin{document}
\begin{tikzpicture}[blabel/.style={midway,anchor=center,fill=white,inner sep=0.5pt}]
\draw (0,0) -- (17,0);
\foreach \x [count=\i] in {0.5,1.5,...,15.5}
{
\draw(\x,0) node[above=5pt] {$x_{\i}$} -- ++(0,5pt);
}
\draw(16.5,0) node[above=5pt] {$x_{n}$} -- ++(0,5pt);
\foreach \x [count=\i] in {1,3,...,15}
{
\draw[|-|,color\x] (\x-0.5,{-0.55-0.25*(-1)^\i}) -- node[blabel,text=color\x] {$B_{\x}$} ++(2,0);
}
\foreach \x [count=\i] in {2,4,...,14}
{
\draw[|-|,color\x] (\x-0.5,{1.05-0.25*(-1)^(\i+1)}) -- node[blabel,text=color\x] {$B_{\x}$} ++(2,0);
}
\end{tikzpicture}
\end{document}
编辑:您还可以使用稍微更“高级”的数学函数组合来组合标签的循环:
\foreach \x in {1,2,...,15}
{
\draw[|-|,color\x] (\x-0.5,{0.55*((-1)^\x)-0.5*(Mod(\x,2)-1)-0.25*(-1)^(int(\x/2)+1)}) -- node[blabel,text=color\x] {$B_{\x}$} ++(2,0);
}
编辑2:为了在小节长度方面有更大的灵活性,你可以使用\pgfmatsetmacro
s:
\begin{tikzpicture}[blabel/.style={midway, anchor=center, fill=white, inner sep=0.5pt}]
\newcommand*{\barlength}{3}%
\pgfmathsetmacro{\barnumber}{16/\barlength}%
\draw (0,0) -- (17,0);
\foreach \x [count=\i] in {0.5,1.5,...,15.5}
{\draw(\x,0) node[above=5pt] {$x_{\i}$} -- ++(0,5pt);}
\draw(16.5,0) node[above=5pt] {$x_{n}$} -- ++(0,5pt);
\foreach \x in {1,2,...,\barnumber}
{
\draw[|-|,color\x] ({\x-0.5+(\x-1)*(\barlength-1)},{0.55*((-1)^\x)-0.5*(Mod(\x,2)-1)-0.25*(-1)^(int(\x/2)+1)}) -- node[blabel,text=color\x] {$B_{\x}$} ++(\barlength,0);
}
\end{tikzpicture}