0
\usepackage{pgfplots}
\usepackage{amsmath}
\include{macros/style}
\include{macros/use_packages}
\usepackage{indentfirst}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{tikz}
\usepackage{array,booktabs,ragged2e}
\usepackage{listings}
\usepackage{color}
\section{Results}
\pgfplotstableread[row sep=\\,col sep=&]{
interval & carT & carD & carC & carG\\
10 Hz     & 62680  & 125130 & 10000 & 100000\\
OSC     & 62680  & 125130 & 100000 & 100000\\
Adaptive     & 62680  & 125130 & 1000000 & 1000000\\
}\mydata

\begin{tikzpicture}
\hspace{-2cm}
\begin{axis}[
        ybar,
        bar width=17pt,
        x=7cm,
        ybar=1pt,
        width=1\textwidth,
        height=.5\textwidth,
        legend style={at={(0.5,1)},
            anchor=north,legend columns=-1},
        symbolic x coords={10 Hz, OSC, Adaptive},
        xtick=data,
        nodes near coords,
        nodes near coords align={horizontal},
        ymin=0,ymax=400000,
        ylabel={Number of Packets Sent},
    ]
    \addplot table[x=interval,y=carT]{\mydata};
    \addplot table[x=interval,y=carD]{\mydata};
    \addplot table[x=interval,y=carC]{\mydata};
    \addplot table[x=interval,y=carG]{\mydata};
    \legend{Six Lanes, 12 Lanes, E.C. Row, i75}
\end{axis}
\end{tikzpicture}

enter image description here

Stefan Pinnow
  • 29,535
LionsFan
  • 15
  • 4
  • 2
    Welcome! Please provide an MWE. –  Jan 26 '18 at 05:16
  • Sorry don't think it is possible. All I want to do is decrease the horizontal separation between two different plots. – LionsFan Jan 26 '18 at 05:24
  • The answer will depend on which style files you are loading and so on. Without that information, I can only guess that you may want to remove the line x=7cm,. –  Jan 26 '18 at 05:26
  • I included the classes I am including in my main file and I added the image as well. Hope that helps? – LionsFan Jan 26 '18 at 05:34
  • @marmot I believe that should give enough MWE, doesn't it? – LionsFan Jan 26 '18 at 05:51
  • No, it doesn't compile because there is no \documentclass, nor \begin{document} nor \end{document}. But I guess you really just need to play with x=7cm,, e.g. by replacing it by x=3.5cm,. –  Jan 26 '18 at 05:53
  • Thank you. But when I do that, it shrinks the size of the plot and the outer bars get cutoff with the border. – LionsFan Jan 26 '18 at 06:04
  • comment out the bar width=17pt that forces the plots to get out of the "supposed" area that they would use (or reduse it to half too) – koleygr Jan 26 '18 at 06:12

2 Answers2

3

For me the key to a good solution is to switch from absolute width (in pt) to relative width using the axis coordinate system. Doing this has the advantage that when you once have fixed the bar widths and "distance of the bars to the axis" (using enlarge x limits) you can scale your plot using width only.

For details please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \pgfplotsset{
        % use this `compat' level or higher to use the feature of specifying
        % `bar width' in axis units
        compat=1.7,
    }
    \pgfplotstableread[row sep=\\,col sep=&]{
        interval & carT  & carD   & carC    & carG    \\
        10 Hz    & 62680 & 125130 & 10000   & 100000  \\
        OSC      & 62680 & 125130 & 100000  & 100000  \\
        Adaptive & 62680 & 125130 & 1000000 & 1000000 \\
    }\mydata
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % it doesn't make sense to specify `x' AND `width', because only
        % one of them can be used. Here `x' would make the race.
%        x=7cm,
        width=1\textwidth,
        height=.5\textwidth,
        ymin=0,
        ymax=400000,
        ylabel={Number of Packets Sent},
        xtick=data,
        % ---------------------------------------------------------------------
        % replace `symbolic x coords' by `xticklabels from table' (step 1)
        % (and do step 2 which is given at the `\addplot's)
%        symbolic x coords={10 Hz, OSC, Adaptive},
        xticklabels from table={\mydata}{interval},
        ybar,
        % when not using symbolic coords we have the opportunity to specify
        % `bar width' in axis units ...
        bar width=0.2,
        ybar=1pt,
        % ... and then we can also give an absolute value for `enlarge x limits'
        enlarge x limits={abs=0.5},
        % Now you can change `width' as you like and don't need to tough
        % here, because everything is given "relative"
        % ---------------------------------------------------------------------
        nodes near coords,
        legend style={
            at={(0.5,1)},
            anchor=north,
            legend columns=-1,
        },
    ]
        % replaced `x=interval' with `x expr=\coordindex' (step 2)
        % to have the same result as using `symbolic x coords'
        \addplot table [x expr=\coordindex,y=carT]{\mydata};
        \addplot table [x expr=\coordindex,y=carD]{\mydata};
        \addplot table [x expr=\coordindex,y=carC]{\mydata};
        \addplot table [x expr=\coordindex,y=carG]{\mydata};

        \legend{Six Lanes, 12 Lanes, E.C. Row, i75}
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
1

i would draw this plots on the following way:

enter image description here

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}
\pgfplotstableread[row sep=\\,col sep=&]{
    interval & carT  & carD   & carC    & carG    \\
    10 Hz    & 62680 & 125130 & 10000   & 100000  \\
    OSC      & 62680 & 125130 & 100000  & 100000  \\
    Adaptive & 62680 & 125130 & 1000000 & 1000000 \\
}\mydata
\begin{document}
\begin{tikzpicture}
\begin{axis}[
     width=1\textwidth,
    height=.5\textwidth,
    ymin=0,
    ymax=1 150 000,
    yticklabel style={/pgf/number format/fixed},
    ytick distance=200 000,
    axis y discontinuity=crunch,
    ylabel={Number of Packets Sent},
    xtick=data,
    xticklabels from table={\mydata}{interval},
    bar width=0.2,
    ybar=2pt,
    enlarge x limits={abs=0.55},
    nodes near coords,
    nodes near coords style={font=\tiny},
    /pgf/number format/.cd,use comma,sci,
    /pgf/number format/sci generic={mantissa sep={\!\!\cdot\!\!},
                                    exponent={10^{####1}}
                                    },
    legend pos=north west,
    legend style={legend columns=-1},
]
    \addplot table [x expr=\coordindex,y=carT]{\mydata};
    \addplot table [x expr=\coordindex,y=carD]{\mydata};
    \addplot table [x expr=\coordindex,y=carC]{\mydata};
    \addplot table [x expr=\coordindex,y=carG]{\mydata};

    \legend{Six Lanes, 12 Lanes, E.C. Row, i75}
\end{axis}
\end{tikzpicture}
\end{document}
Zarko
  • 296,517