3

I am trying to make a two bar plots together just like below, but with an xbar instead.

enter image description here

However, this is proving to be harder than required for me. Here is my code:

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage{pgfplots}
\pgfplotsset{width=0.8\linewidth,compat=1.17}

\begin{document}
    
    \begin{tikzpicture}% table
        \begin{axis}[
            xbar,
            bar width=0.7,
            xlabel=Quantidade de vendedores/consumidores,
            ylabel=Estado,
            ytick=data,
            yticklabels from table={data/num_sellers_per_state.dat}{State},
            enlargelimits=0.05,
            legend pos=south east,
        ]
            
            % Customer data
            \addplot table[
                y expr=\coordindex,
                x=CustomerSize,
            ] {data/num_sellers_per_state.dat};
            
            % Seller data
            \addplot table[
                y expr=\coordindex,
                x=SellerSize,
            ] {data/num_sellers_per_state.dat};
            
            \legend{Consumidores,Vendedores}
        \end{axis}
    \end{tikzpicture}
    
\end{document}

and here are the contents of the data/num_sellers_per_state.dat file:

State   CustomerSize    SellerSize
RR  46  0
AP  68  0
AC  81  1
AM  148 1
RO  253 2
TO  280 0
SE  350 2
AL  413 0
RN  485 5
PI  495 1
PB  536 6
MS  715 5
MA  747 1
MT  907 4
PA  975 1
CE  1336    13
PE  1652    9
GO  2020    40
ES  2033    23
DF  2140    30
BA  3380    19
SC  3637    190
PR  5045    349
RS  5466    129
MG  11635   244
RJ  12852   171
SP  41746   1849

What I end up receiving is a plot with the bars on top of each other, as shown below.

enter image description here

I tried setting bar width and bar shift properties, but nothing works. I looked for multiple questions relative to this but they all seem to work for ybars only.

Edit: Fix typos.

1 Answer 1

4

Welcome to TeX.SX! You actually only need to increase the height of your plot or decrease the width of the bars (or better yet, both):

\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{filecontents}{num_sellers_per_state.dat}
State   CustomerSize    SellerSize
RR  46  0
AP  68  0
AC  81  1
AM  148 1
RO  253 2
TO  280 0
SE  350 2
AL  413 0
RN  485 5
PI  495 1
PB  536 6
MS  715 5
MA  747 1
MT  907 4
PA  975 1
CE  1336    13
PE  1652    9
GO  2020    40
ES  2033    23
DF  2140    30
BA  3380    19
SC  3637    190
PR  5045    349
RS  5466    129
MG  11635   244
RJ  12852   171
SP  41746   1849
\end{filecontents}
    
\begin{document}

\begin{tikzpicture}% table
    \begin{axis}[
        height=15cm,
        xbar,
        bar width=.15cm,
        xlabel=Quantidade de vendedores/consumidores,
        ylabel=Estado,
        ytick=data,
        yticklabels from table={num_sellers_per_state.dat}{State},
        enlargelimits=0.05,
        legend pos=south east,
    ]
        
        % Customer data
        \addplot table[
            y expr=\coordindex,
            x=CustomerSize,
        ] {num_sellers_per_state.dat};
        
        % Seller data
        \addplot table[
            y expr=\coordindex,
            x=SellerSize,
        ] {num_sellers_per_state.dat};
        
        \legend{Consumidores,Vendedores}
    \end{axis}
\end{tikzpicture}
    
\end{document}

(Note, I changed the path to the file in the code in order to make it work with filecontents.)

enter image description here

1
  • 1
    Well, that was silly of me :). I started incorrectly using xbar interval with the same values and it worked with no overlap; but of course, other stuff were breaking because, well, I was using it incorrectly. So increasing the image height never crossed my mind! Thanks for the quick answer and for the welcome! Commented Dec 20, 2021 at 2:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .