我有这个代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{patterns}
\definecolor{trolleygrey}{rgb}{0.5, 0.5, 0.5}
\definecolor{darkgray}{rgb}{0.66, 0.66, 0.66}
\title{pgfplots}
\author{antonis.makris2411 Mak}
\date{July 2019}
\newcounter{cheat}
\begin{document}
\maketitle
\begin{figure*}[!htbp]
\begin{tikzpicture}
\pgfplotsset{
/pgfplots/bar shift auto/.style={
/pgf/bar shift={
-0.5*(int(3/2*\numplotsofactualtype-1)*\pgfplotbarwidth
+ (int(3/2*\numplotsofactualtype-1))*(#1)) +
(.5+\plotnumofactualtype+int(\plotnumofactualtype/2))*\pgfplotbarwidth
+ \plotnumofactualtype*(#1)
},
},
A bar/.style={nodes near coords={\stepcounter{cheat}%
\pgfmathparse{pow(10,int((1+\number\value{cheat})/2))}%
\pgfmathprintnumber\pgfmathresult},
style={trolleygrey,fill=trolleygrey,mark=none},postaction={pattern=crosshatch
dots}},
B bar/.style={darkgray,fill=darkgray,mark=none}
}
\begin{axis}[
width = 0.9*\textwidth,
major x tick style = transparent,
ybar=2*\pgflinewidth,
bar width=0.4cm,
ymajorgrids = true,
ylabel = {Response Time (sec)},
symbolic x coords={P1,P2},
xtick = data,
scaled y ticks = false,
enlarge x limits=0.35,
x tick label style={font=\Large,yshift=-10pt},
y tick label style={font=\Large},
y label style={font=\Large},
ymin=0,
legend cell align=left,
legend style={font=\Large,draw=none, legend columns=-1},
visualization depends on=y \as \rawy,
every node near coord/.append style={% https://tex.stackexchange.com/a/110887/121799
anchor=north,xshift=0.2cm,
shift={(axis direction cs:P1,-\rawy)}
}
]
\addplot[A bar]
coordinates {(P1, 104.09) (P2,685.37)};
\addplot[B bar]
coordinates {(P1, 103.43) (P2,364.37)};
\addplot[A bar]
coordinates {(P1,495.74) (P2,454.20)};
\addplot[B bar]
coordinates {(P1, 364.43) (P2,364.37)};
\addplot[A bar]
coordinates {(P1,1929.74) (P2,454.20)};
\addplot[B bar]
coordinates {(P1,699.54) (P2,454.20)};
\legend{S1, S2}
\end{axis}
\end{tikzpicture}% pic 1
\end{figure*}
\end{document}
产生如下结果:
这段代码:
\pgfplotsset{
/pgfplots/bar shift auto/.style={
/pgf/bar shift={
-0.5*(int(3/2*\numplotsofactualtype-1)*\pgfplotbarwidth
+ (int(3/2*\numplotsofactualtype-1))*(#1)) +
(.5+\plotnumofactualtype+int(\plotnumofactualtype/2))*\pgfplotbarwidth
+ \plotnumofactualtype*(#1)
},
}
设置 P1-10、P1-100、P1-1000 各条杆之间的距离。
因为我刚接触 pgfplots,所以我不知道如何增加这个距离。要更改哪个值?
答案1
请改用此定义:
\pgfplotsset{
/pgfplots/bar shift auto/.style={
/pgf/bar shift={
- \distb * \dista * 0.5*(int(3/2*\numplotsofactualtype-1)*\pgfplotbarwidth
+ \distb * (int(3/2*\numplotsofactualtype-1))*(#1))
+ \distb * \dista * (.5+\plotnumofactualtype+int(\plotnumofactualtype/2))*\pgfplotbarwidth
+ \distb * \plotnumofactualtype*(#1)
},
},
它有两个额外的“参数”(实际上是全局定义的宏)distb
和dista
。
将其放在 tikzpicture 上方:
\def\dista{1.0}
\def\distb{1.0}
一个增加深灰色图和浅灰色图之间的距离,另一个增加深灰色图和浅灰色图之间的距离。为两者找到适合您偏好的值,因为我不太确定您想要什么。
我没有完全掌握该bar shift auto
功能 - 我的修改版本是基于我的(有点有限的)理解。