我想用一条从一个节点到另一个节点的线将我的多系列条形图的每个系列的条形连接起来。
因此,所有蓝色条都与一条蓝线相连,所有橙色条都与一条橙色线相连。但是,我无法做到这一点。有没有办法将绘图类型从 ybar 设置为线?
解决方案关于此主题的先前问题由于我处理多个系列,因此似乎不起作用。
\begin{figure}[!!h]
\centering
\pgfplotstableread{
1 1.64 6.27
2 5.67 -27.13
3 3.58 -9.36
4 6.25 -10.74
}\dataset
\begin{tikzpicture}
\centering
\begin{axis}[ybar,
ymin=-32,
ymax=25,
xmin=0.6,
xmax=4.4,
ylabel={Return in \% },
bar width=20pt,
xtick=data,
enlargelimits=true,
xticklabels ={Pre(0),Pre(1),Crisis(2),Post(3)},
nodes near coords,
major x tick style = {opacity=0},
xticklabel style=
{rotate=45,anchor=near xticklabel},
minor x tick num = 1,
minor tick length=2ex,
]
\addplot[draw=blue,fill=blue!20] table[x index=0,y index=1] \dataset; %Data1
\addplot[draw=orange,fill=orange!40] table[x index=0,y index=2] \dataset; %Data2
\legend{First Day MAR, 36 months BHAR};
\end{axis}
\end{tikzpicture}
\caption{Averages per issue period}
\end{figure}
答案1
我现在能想到的最好的解决方案是复制图,但不使用选项fill
,然后添加smooth
(或sharp
)使用一条线。
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\pgfplotstableread{
1 1.64 6.27
2 5.67 -27.13
3 3.58 -9.36
4 6.25 -10.74
}\dataset
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=-32,
ymax=25,
xmin=0.6,
xmax=4.4,
ylabel={Return in \% },
bar width=20pt,
xtick=data,
enlargelimits=true,
xticklabels ={Pre(0),Pre(1),Crisis(2),Post(3)},
nodes near coords,
major x tick style = {opacity=0},
xticklabel style=
{rotate=45,anchor=near xticklabel},
minor x tick num = 1,
minor tick length=2ex,
]
\addplot[draw=blue,fill=blue!20] table[x index=0,y index=1] \dataset; %Data1
\addplot[draw=orange,fill=orange!40] table[x index=0,y index=2] \dataset; %Data2
\addplot[smooth, draw=blue] table[x index=0,y index=1] \dataset; %Data1
\addplot[smooth, draw=orange] table[x index=0,y index=2] \dataset; %Data2
\legend{First Day MAR, 36 months BHAR};
\end{axis}
\end{tikzpicture}
\end{document}