I added a new axis to my barplot and that has a different scale as the first one.
The second axis is shifted and does not have the same height as the first one.
What do I need to change so that the right Y-Axis is not shifted and the line goes from the left to the right axis?
My Code so far:
\documentclass[12pt,tikz, border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width = \textwidth,
height = 9cm,
major x tick style = transparent,
ybar=2*\pgflinewidth,
bar width=14pt,
ymajorgrids = true,
ylabel = {Time},
xlabel = {Subject},
symbolic x coords={1,2,3,4,5},
xtick = data,
scaled y ticks = false,
enlarge x limits=0.25,
ymin=0,
legend columns=2,
legend cell align=left,
legend style={
at={(0.5,-0.2)},
anchor=north,
column sep=1ex
}
]
\addplot[style={blue,fill=blue,mark=none}]
coordinates {(1, 09.06) (2, 09.52) (3, 12.20) (4, 12.10) (5, 14.05)};
\addplot[style={red,fill=red,mark=none}]
coordinates {(1, 19.22) (2, 21.29) (3, 21.58) (4, 23.27) (5, 37.03)};
\legend{Data1, Data2}
\end{axis}
\begin{axis}[
scale only axis,
axis y line*=right,
axis x line=none,
width = \textwidth,
height = 9cm,
major x tick style = transparent,
ybar=2*\pgflinewidth,
enlarge x limits=0.25,
bar width=14pt,
ylabel = {Advantage},
xmin=0, xmax=6,
ymin=0, ymax=4,
legend columns=2,
legend cell align=left,
legend style={
at={(0.5,-0.2)},
anchor=north,
column sep=1ex
}
]
\addplot[style={green,fill=green,mark=none}]
coordinates {(1, 2) (2, 2) (3, 2) (4, 2) (5,2)};
\addplot[green,sharp plot,update limits=false] coordinates {(0,1.5) (6,1.5)} node[above] at (axis cs:3,1.5) {1.5};
\legend{advantage}
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
You have to use same attributes to the y ticks in both the axis like
and remove
scale only axis,
too. Further, you have to plot all bars inside the sameaxis
environment so that they are properly spaced. I have commented out the lines that are not necessary and we keep the secondaxis
environment just for the right y axis. Also, I have drawn the second x axis and usedaxis on top
so that bars don't cover the x axis. So many changes that I can't write on all of them. Please see the code.