6

I am having two issues with TikZ pictures. I have three graphs side by side, and I am having issues, but only with some of them, which is very odd. First the x-axis is going out of alignment (some graphs 0 is starting above the x axis). Further, sometimes they are appearing on the right side of the page (all 3 as a whole). Does anyone know what's up?

enter image description here

enter image description here

\usepackage[numbers]{natbib}
\usepackage{graphicx}
\usepackage{url}
\usepackage{mathtools}
\usepackage{listings}
\usepackage{afterpage}
\usepackage{subcaption}

\usepackage{tikz}
\usepackage{pgfplots}

\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true}

\begin{figure}[tbp]
\centering
\begin{subfigure}{0.26\linewidth}
\caption{Average Playback Version}
\begin{center}
\begin{tikzpicture}[scale=0.8]
\begin{axis}[
%x tick label style={/pgf/number format/1000 sep=},
xlabel=Available Bandwidth (Mbps),
xtick={0, 1,  2},
xticklabels={0.5, 1, 2.375},
enlargelimits=0.15,
legend style={at={(0.5,1.0)},anchor=north,legend columns=-1},
ybar,
bar width=5pt,
ymin=0,
]
\addplot
    coordinates{(0,1.0) (1,2.730392156862745) (2,5.5777777777777775)}; %no prefetching
\addplot
    coordinates{(0,2.227722772277228) (1,4.368932038834951)(2,5.821256038647343)}; %1ahead
\addplot
    coordinates{(0,3.308080808080808) (1,5.458128078817734) (2,5.894230769230769)}; %2ahead
\addplot
    coordinates{(0,3.645320197044335) (1,5.579710144927536) (2,5.91866028708134)}; %3ahead
\addplot
    coordinates{(0,3.6161616161616164) (1,5.586538461538462) (2,5.913461538461538)}; %4ahead
\legend{0,1,2,3,4}
\end{axis}
\end{tikzpicture}
\end{center}
\end{subfigure}
\hfill
\begin{subfigure}{0.26\linewidth}
\caption{Playback Smoothness}
\begin{center}
\begin{tikzpicture}[scale=0.8]
\begin{axis}[
%x tick label style={/pgf/number format/1000 sep=},
xlabel=Available Bandwidth (Mbps),
xtick={0, 1,  2},
xticklabels={0.5, 1, 2.375},
enlargelimits=0.15,
legend style={at={(0.5,1.0)},anchor=north,legend columns=-1},
ybar,
bar width=5pt,
ymin=0,
]
\addplot
    coordinates{(0,1.0) (1,0.35290032443312624) (2,0.3632628202048895)}; %no prefetching
\addplot
    coordinates{(0,0.054394041151172384) (1,0.07615289788041628)(2,0.22011558857202693)}; %1ahead
\addplot
    coordinates{(0,0.03554400773960198) (1,0.14257504150852274) (2,0.3774166052065033)}; %2
\addplot
    coordinates{(0,0.03961959500334247) (1,0.16638478387140487) (2,0.3830491443385205)}; %3
\addplot
    coordinates{(0,0.019048507238026372) (1,0.15373343226268732) (2,0.38489573116089204)};   %4
\legend{0,1,2,3,4}
\end{axis}
\end{tikzpicture}
\end{center}
\end{subfigure}
\hfill
\begin{subfigure}{0.26\linewidth}
\caption{Prefetching Overhead}
\begin{center}
\begin{tikzpicture}[scale=0.8]
\begin{axis}[
%x tick label style={/pgf/number format/1000 sep=},
xlabel=Available Bandwidth (Mbps),
xtick={0, 1, 2},
xticklabels={0.5, 1, 2.375},
enlargelimits=0.15,
legend style={at={(0.5,1.0)},anchor=north,legend columns=-1},
ybar,
bar width=5pt,
ymin=0,
]
\addplot
    coordinates{(0,0.0) (1,0) (2,0)}; %no prefetching
\addplot
    coordinates{(0,0.1419330653262516) (1,0.20864903902261225)(2,0.08526972243645012)};
\addplot
    coordinates{(0,0.30093283802483317) (1,0.15273218048993692) (2,0.05025336703845387)};
\addplot
    coordinates{(0,0.4714792928713229) (1,0.1685468393484924) (2,0.04331154050139798)};
\addplot
    coordinates{(0,0.8938421340289666) (1,0.2328490684012425) (2,0.055865530184037435)};    
\legend{0,1,2,3,4}
\end{axis}
\end{tikzpicture}
\end{center}
\end{subfigure}
\caption{PC Browser: Constant Available Bandwidth}
\end{figure}
Ludovic C.
  • 8,888
Mike
  • 61
  • With positive bar plots, you should include ymin = 0 in your axis options (that will fix the issue with the axis starting at the wrong position) – Jake Sep 22 '13 at 16:20
  • @Jake I've now included ymin=0, in all axis options but that does not seem to have helped (although it made the results consistently off the x-axis like my second picture, rather htan the first). Is it possible that it is related to some of my bars having a 0 value? – Mike Sep 22 '13 at 16:25
  • Sorry, I should have mentioned that you either need to add the ymin=0 after enlargelimits=0.15, or you should set enlargelimits=upper (otherwise the lower limit will be adjusted, which you don't want in this case). – Jake Sep 22 '13 at 16:27
  • @Jake Thanks! That appears to have solved that problem! Only the graph alignment is off now and I believe that is the result of the y-axis labels being 5x10^2 in the case of the second graph, which increases the gap between graphs. Not sure how to fix it but at least I know the issue. – Mike Sep 22 '13 at 16:47
  • You can change the number format of the y axis to make sure that all numbers are printed as fixed point (not scientific). See http://tex.stackexchange.com/questions/96347/pgfplots-number-format-on-axis – Jake Sep 22 '13 at 16:52
  • @Jake Thank you for the pointer. It seems like enlargelimits=upper does solve my problem with the bars starting off the x-axis but it has introduced another problem where the bars are too wide. (eg the bars are pushed over the left and right edges of the graph so they cant be seen.) – Mike Sep 22 '13 at 17:14
  • Try enlarge x limits=0.15, enlarge y limits=upper. That way, the x range stays the same as it currently is. – Jake Sep 22 '13 at 17:17
  • @Jake Thanks!! You're help has been much appreciated! As a final question unrelated to my problem. Is there anyway I can move my x tick marks closer together to make the figure narrower so the graphs fit nicer with wider margins on the page? – Mike Sep 22 '13 at 19:44

1 Answers1

3

Set enlarge x limits=0.15, enlarge y limits=upper. That way, the x range stays the same as it currently is, and the bars start from the lower edge of the plot area.

Jake
  • 232,450