[Tex/LaTex] How to do \flushleft and \flushright with tikzpictures

horizontal alignmentpgf-piepgfplots

I have two charts, one is drawn with pgfplots, the other with pgf-pie. I want put these two chart in align horizontally, so one is on the left and the other is on the right. I have read Aligning using flushleft and flushright, but it seems this minipage trick does not work in my case.

This is an example. I expected the two charts stand side-by-side, but they are not. About the width of the minipage, I tried many values. Maybe I don't understand it correctly.

\begin{minipage}{0.5 \linewidth}
\begin{flushleft}
\begin{tikzpicture}
  \begin{axis}[
    title=\# of players in a class,
    ybar,
    %enlargelimits=0.15,
    legend style={at={(0.5,-0.2)},
      anchor=north,legend columns=-1},
    ylabel={Population},
    symbolic x coords={Warrior,Wizard,Archer},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical},
    x tick label style={rotate=45,anchor=east},
  ]
    \addplot coordinates {(Warrior,100) (Wizard,100) (Archer,200)};
  \end{axis}
\end{tikzpicture}
\end{flushleft}
\end{minipage}

\begin{minipage}{0.5 \linewidth}
\begin{flushright}
Ratio of players in a class
\begin{tikzpicture}
  \pie{ 25/ \textsc{Warrior} , 25/ \textsc{Wizard} , 50/ \textsc{Archer} }
\end{tikzpicture}
\end{flushright}
\end{minipage}

Best Answer

The minipage trick still does work, but

  1. you have spurious spaces (and a paragraph!) between the minipages, so they don't end up side-by-side and
  2. the figures are too large to fit in boxes of width 0.5\linewidth.

To fix Problem 1: Remove the newlines and add comments at ends of lines to suppress spurious spaces. You also need to add \hfill between the minipages as in the linked solution.

To fix Problem 2: You can adjust the width key in the axis environment to change the size of the plot. You can also use the scale option to the tikzpicture to adjust the size of the pie chart.

\documentclass{article}
\usepackage{pgfplots,pgf-pie,lipsum}
\pgfplotsset{compat=1.10}
\fboxsep=0pt % just for showing minipage size

\begin{document}\noindent
\fbox{% just to show minipage size
\begin{minipage}[b]{0.45 \linewidth}% just slightly shrunk to show the separation
\begin{flushleft}
\begin{tikzpicture}
  \begin{axis}[
    title=\# of players in a class,
    ybar,
    width=5cm, % adjust here
    enlargelimits=0.25,
    legend style={at={(0.5,-0.2)},
      anchor=north,legend columns=-1},
    ylabel={Population},
    symbolic x coords={Warrior,Wizard,Archer},
    xtick=data,
    nodes near coords,
    nodes near coords align={vertical},
    x tick label style={rotate=45,anchor=east},
  ]
    \addplot coordinates {(Warrior,100) (Wizard,100) (Archer,200)};
  \end{axis}
\end{tikzpicture}
\end{flushleft}
\end{minipage}}\hfill%
\fbox{% just to show minipage size
\begin{minipage}[b]{0.45 \linewidth}% just slightly shrunk to show the separation
\begin{flushright}
Ratio of players in a class
\begin{tikzpicture}[scale=0.5]% adjust scale parameter
  \pie{ 25/ \textsc{Warrior} , 25/ \textsc{Wizard} , 50/ \textsc{Archer} }
\end{tikzpicture}%
\end{flushright}%
\end{minipage}}%

\lipsum[1-2] % just to show margins
\end{document}

enter image description here

You can remove the \fboxes for your own usage; I've just used them here to show the size of the minipages.