我想为我编写的 pgfplot 条形图添加重要性条形图(如下图所示)。到目前为止,我已经使用“节点”工作,只是添加星号,但更喜欢条形图。
感谢您的帮助 :-)
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.7}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{wrapfig}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\usepackage{lipsum}
\title{Example}
\author{Name }
\date{April 2021}
\begin{document}
\maketitle
\section{Introduction}
\begin{figure*}[ht]
\centering
\resizebox{\textwidth}{!}{%
\begin{subfigure}[pt]{0.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
ybar,
%bar width=.5cm,
domain=0:1,
every axis plot/.append style={no markers},
%xlabel=Timepoints (d),
ylabel= Frequency of Bursts,
width=\textwidth,
height=7cm,
ymax=6,
ymin=1,
xmin=-0.5,
xmax=1.5,
xticklabels={DIV15, DIV17},xtick={0,1},
x tick label style={rotate=90},
legend style={at={(0.025,0.875)},anchor=west,legend columns=2}]
%NMC
%NMC
\addplot+[fill,error bars/.cd,
y dir=both,y explicit]
coordinates {%NmC H
(0,3.7) +- (0.17,0.17)
(1,4.2) +- (0.28,0.28)
};
\addplot+[fill,error bars/.cd,
y dir=both,y explicit]
coordinates {%aS L
(0,3.3) +- (0.4,0.4)
(1,3.4) +- (0.25,0.25)
};
\addplot+[fill,error bars/.cd,
y dir=both,y explicit]
coordinates {%NmC H
(0,3.7) +- (0.17,0.17)
(1,4.1) +- (0.18,0.18)
};
\addplot+[fill,error bars/.cd,
y dir=both,y explicit]
coordinates {% aS H
(0,2.3) +- (0.1,0.1)
(1,3.1) +- (0.35,0.35)
};
%NMC
%NMC
%\node [above, font=\Large] at (axis cs: 0.21,300) {$\ast$};
%\node [above, font=\Large] at (axis cs: 1.21,265) {$\ast$};
\legend{\scriptsize{NmCherry L},\scriptsize{$\alpha$-syn L},\scriptsize{NmCherry H},\scriptsize{$\alpha$syn H}}
\end{axis}
\end{tikzpicture}
%\caption{Cell viability post-treatment}
%\label{fig:sub1}
\end{subfigure}
\hspace{8mm}
\begin{subfigure}[pt]{0.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
ybar,
%bar width=.5cm,
domain=0:1,
every axis plot/.append style={no markers},
%xlabel=Timepoints (d),
ylabel= \% of Active Neurones,
width=\textwidth,
height=7cm,
ymax=80,
ymin=10,
xmin=-0.5,
xmax=1.5,
xticklabels={DIV15, DIV17},xtick={0,1},
x tick label style={rotate=90},
legend style={at={(0.45,0.78)},anchor=west}]
%# of BURSTS
%# of BURSTS
\addplot+[fill,error bars/.cd,%NmC L
y dir=both,y explicit]
coordinates {
(0,58) +- (3.7,3.7)
(1,65) +- (1.9,1.9)
};
\addplot+[fill,error bars/.cd,%aS L
y dir=both,y explicit]
coordinates {
(0,30) +- (3.4,3.4)
(1,51) +- (2.9,2.9)
};
\addplot+[fill,error bars/.cd,%NmC H
y dir=both,y explicit]
coordinates {
(0,57) +- (4.2,4.2)
(1,60) +- (3.5,3.5)
};
\addplot+[fill,error bars/.cd,%aS H
y dir=both,y explicit]
coordinates {
(0,19) +- (2.9,2.9)
(1,39) +- (3.8,3.8)
};
%\node [above, font=\Large] at (axis cs: 0.21,2.8) {$\ast$};
%\node [above, font=\Large] at (axis cs: 1.21,3.6) {$\ast$};
%# of BURSTS
%# of BURSTS
%\legend{\scriptsize{NmCherry$^{+}$-Low},\scriptsize{$\alpha$syn-NmC$^{+}$-Low},\scriptsize{NmCherry$^{+}$-High},\scriptsize{$\alpha$syn-NmC$^{+}$-High}}
\end{axis}
\end{tikzpicture}
%\caption{Network burst activity post-treatment}
%\label{fig:sub1}
\end{subfigure}}
%\decoRule \\
\caption[]{\\
%\decoRule \\
\footnotesize{\textit{.}}}
\label{fig:3.52}
\end{figure*}
\end{document}
答案1
完整的代码在最后的一小段文字之后。
好吧,从 a 到 be 画一条线与在普通 TikZ 中相同:
\draw (a) -- (b); % if a and be are named coordinates
\draw (axis cs:0,0) -- (axis cs:1,1); % for explicit coordinates
如果您有compat=1.11
或更高版本(您当前有 1.7),则将axis cs:
成为默认版本,因此\draw (0,0) -- (1,1);
将执行与上述相同的操作。
要在该线的中间上方添加节点,请执行以下操作
\draw (0,0) -- node[above] {foo} (1,1);
对于最后的“钩子”,你可以这样做https://tex.stackexchange.com/a/203830/,或者使用 手动绘制它们\draw (0,-3pt) |- (1,0) -- (1,-3pt);
,网站上可能还有其他解决方案。在下面的代码中,我使用arrows.meta
库定义了一种新的线条样式,
\tikzset{
% define new arrow tip, which is just a straight line
bar/.tip={Rectangle[length=1pt, width=3pt]},
% use the new arrow tip, but only on one side of the line
% left/right is when standing in the middle of the line looking towards
% the end of the line, hence right on one and left on the other
signbarL/.style={
{bar[right]}-{bar[left]}
}
}
这并不太好,因为线必须从右到左画,但对于这种情况来说是可行的。
到目前为止,您只需要弄清楚要使用哪些坐标。对于 y 坐标,我猜只需查看图表,例如,第一个坐标介于 4 和 4.5 之间似乎没问题。
对于 x 坐标,您可以进行一些反复试验来找到合适的值,同时知道两个组的中心位于 0 和 1。或者利用条形的宽度保存的事实\pgfplotbarwidth
,并且条形之间的空间为 2pt(除非您更改它)。
直接在显式坐标中使用\pgfplotbarwidth
不起作用,但它适用于相对坐标,因此使用类似的构造
\draw [signbarL] (axis cs:0,4.1) % set point at middle of first bar group, at y=4.1
++(2*\pgfplotbarwidth+3pt, 0) % move point 2 bar widths + 1.5 bar space to the right, without drawing a line
-- % draw a line to the next coordinate
+(-2*\pgfplotbarwidth-2pt, 0); % end point is 2 bar widths and one bar space left of the previous point
\draw [signbarL] (axis cs:0.235,4.45) --node[above]{$\ast$} (axis cs:-0.235, 4.45);
如果合乎逻辑的话,可能有点复杂。如上所述,如果您愿意的话,您可以进行一些反复试验以得出例如。
还有一些其他的事情:pgfplotstable
加载pgfplots
,加载tikz
,加载graphicx
,所以严格来说,你只需要加载其中的第一个。(我认为,如果一个包已经加载,加载其余的包并没有什么坏处,如果我没记错的话,第二次什么也不会发生。)
对于(第一个) 可选参数的有效选项是、、(默认)和subfigure
之一,因此是无效的。不是像 这样的浮动环境,可选参数与环境在基线上的锚定方式有关,而不是与选择页面上允许的位置有关。 当然,如果您不添加子标题,则根本不需要环境。b
B
c
t
T
pt
subfigure
figure
subfigure
最后,虽然我发现将所有内容转储到文本区域中以\resizebox
使其适合文本区域很容易,但我个人还是会将width
每个内容设置axis
为合理的值,这样就不会进行额外的缩放。
抱歉,我发牢骚了:),完整代码如下。
\documentclass{article}
\usepackage{pgfplotstable} % loads pgfplots which loads tikz which loads graphicx
\pgfplotsset{compat=1.7}
\usetikzlibrary{arrows.meta}
\usepackage{subcaption}
\usepackage{wrapfig}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\tikzset{
bar/.tip={Rectangle[length=1pt, width=3pt]},
signbarL/.style={
{bar[right]}-{bar[left]}
}
}
\begin{document}
\begin{figure*}[ht]
% \centering % doesn't do much, since the content is \textwidth anyways
\resizebox{\textwidth}{!}{%
\begin{subfigure}{0.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
ybar,
%bar width=.5cm,
domain=0:1,
every axis plot/.append style={no markers},
%xlabel=Timepoints (d),
ylabel= Frequency of Bursts,
width=\textwidth,
height=7cm,
ymax=6,
ymin=1,
xmin=-0.5,
xmax=1.5,
xticklabels={DIV15, DIV17},xtick={0,1},
x tick label style={rotate=90},
legend style={
at={(0.025,0.875)},
anchor=west,
legend columns=2,
nodes={font=\scriptsize}
}]
%NMC
%NMC
\addplot+[fill,error bars/.cd,
y dir=both,y explicit]
coordinates {%NmC H
(0,3.7) +- (0.17,0.17)
(1,4.2) +- (0.28,0.28)
};
\addplot+[fill,error bars/.cd,
y dir=both,y explicit]
coordinates {%aS L
(0,3.3) +- (0.4,0.4)
(1,3.4) +- (0.25,0.25)
};
\addplot+[fill,error bars/.cd,
y dir=both,y explicit]
coordinates {%NmC H
(0,3.7) +- (0.17,0.17)
(1,4.1) +- (0.18,0.18)
};
\addplot+[fill,error bars/.cd,
y dir=both,y explicit]
coordinates {% aS H
(0,2.3) +- (0.1,0.1)
(1,3.1) +- (0.35,0.35)
};
%NMC
%NMC
%\node [above, font=\Large] at (axis cs: 0.21,300) {$\ast$};
%\node [above, font=\Large] at (axis cs: 1.21,265) {$\ast$};
\draw [signbarL] (axis cs:0,4.1) ++(2*\pgfplotbarwidth+3pt, 0) -- +(-2*\pgfplotbarwidth-2pt, 0);
\draw [signbarL] (axis cs:0,4.25) ++(2*\pgfplotbarwidth+3pt, 0) -- +(-3*\pgfplotbarwidth-4pt, 0);
\draw [signbarL] (axis cs:0,4.4) ++(2*\pgfplotbarwidth+3pt, 0) --node[above]{$\ast$} +(-4*\pgfplotbarwidth-6pt, 0);
\legend{NmCherry L,$\alpha$-syn L,NmCherry H,$\alpha$syn H}
\end{axis}
\end{tikzpicture}
% \caption{Cell viability post-treatment}
% \label{fig:sub1}
\end{subfigure}
\hspace{8mm}
\begin{subfigure}{0.5\textwidth}
\begin{tikzpicture}
\begin{axis}[
ybar,
%bar width=.5cm,
domain=0:1,
every axis plot/.append style={no markers},
%xlabel=Timepoints (d),
ylabel= \% of Active Neurones,
width=\textwidth,
height=7cm,
ymax=80,
ymin=10,
xmin=-0.5,
xmax=1.5,
xticklabels={DIV15, DIV17},xtick={0,1},
x tick label style={rotate=90},
legend style={at={(0.45,0.78)},anchor=west}]
%# of BURSTS
%# of BURSTS
\addplot+[fill,error bars/.cd,%NmC L
y dir=both,y explicit]
coordinates {
(0,58) +- (3.7,3.7)
(1,65) +- (1.9,1.9)
};
\addplot+[fill,error bars/.cd,%aS L
y dir=both,y explicit]
coordinates {
(0,30) +- (3.4,3.4)
(1,51) +- (2.9,2.9)
};
\addplot+[fill,error bars/.cd,%NmC H
y dir=both,y explicit]
coordinates {
(0,57) +- (4.2,4.2)
(1,60) +- (3.5,3.5)
};
\addplot+[fill,error bars/.cd,%aS H
y dir=both,y explicit]
coordinates {
(0,19) +- (2.9,2.9)
(1,39) +- (3.8,3.8)
};
%\node [above, font=\Large] at (axis cs: 0.21,2.8) {$\ast$};
%\node [above, font=\Large] at (axis cs: 1.21,3.6) {$\ast$};
%# of BURSTS
%# of BURSTS
%\legend{\scriptsize{NmCherry$^{+}$-Low},\scriptsize{$\alpha$syn-NmC$^{+}$-Low},\scriptsize{NmCherry$^{+}$-High},\scriptsize{$\alpha$syn-NmC$^{+}$-High}}
\end{axis}
\end{tikzpicture}
%\caption{Network burst activity post-treatment}
%\label{fig:sub1}
\end{subfigure}}
%\decoRule \\
\caption[]{\\
%\decoRule \\
\footnotesize{\textit{.}}}
\label{fig:3.52}
\end{figure*}
\end{document}