Pandas Bar Graph: Side By Side (No X or Y axises shared)
Question: [Two Bar Plots- Non side by side](http://i.stack.imgur.com/XFt3A.png)

Hi, I am relatively new to Pandas and Python. I have been trying to get my two
bar graphs to be displayed side by side instead of showing it one after
another as it is in the image above. They do not share a y or x axis as they
are separate independent variables. I would like to present it so that I can
emphasize how closely related they are and that they are duplicate variables.

So far, I have looked at <http://worksofscience.net/matplotlib/gridspec> where
GridSpec is used to create different grids to plot in. After creating two
grids of equal size ax1 and ax2, I tried to add in the my two plots appended
to their respective variables as with the code below

    
    
    fig1 = plt.figure(figsize=[15,8])
    gs = GridSpec(100,100)
    ax1 = fig1.add_subplot(gs[:,0:50])
    ax2 = fig1.add_subplot(gs[:,51:100])
    
    #saving the plots in variables
    waterpointtype=waterindep.waterpoint_type.value_counts().plot(kind='bar',title ='Waterpoint_Type')
    #plt.show()
    waterpointtypegroup = waterindep.waterpoint_type_group.value_counts().plot(kind='bar',title='Waterpoint_Type_Group')
    #plt.show()
    #plotting on those axes
    ax1.plot(waterpointtype)
    ax2.plot(waterpointtypegroup)
    
    fig1.savefig('waterpointcomparison.png', format ='png', dpi =600)
    plt.show()
    

However, I am ending up with **Index Error: index 51 is out of bounds for axis
0 with size 3** which I cannot make sense of. If you have a solution to this
error or another way by which I can plot bar-graphs side by side, I would
really appreciate it. Thanks a lot!


Answer: Pass the axes to the plot calls:

    
    
    waterindep['waterpoint_type'].value_counts().plot(
        kind='bar',title='Waterpoint_Type', ax=ax1)
    waterindep['waterpoint_type_group'].value_counts().plot(
        kind='bar',title='Waterpoint_Type_Group', ax=ax2)
    

* * *
    
    
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec
    np.random.seed(2015)
    
    fig1 = plt.figure(figsize=[15,8])
    gs = gridspec.GridSpec(100,100)
    ax1 = fig1.add_subplot(gs[:,0:50])
    ax2 = fig1.add_subplot(gs[:,51:100])
    waterindep = pd.DataFrame(np.random.randint(10, size=(100,2)), 
                              columns=['waterpoint_type', 'waterpoint_type_group'])
    waterindep['waterpoint_type'].value_counts().plot(kind='bar',title='Waterpoint_Type', ax=ax1)
    waterindep['waterpoint_type_group'].value_counts().plot(kind='bar',title='Waterpoint_Type_Group', ax=ax2)
    plt.show()
    

## [![enter image description
here](http://i.stack.imgur.com/DbLvW.png)](http://i.stack.imgur.com/DbLvW.png)

Note that you could also use

    
    
    fig, axs = plt.subplots(figsize=[15,8], ncols=2)
    ax1, ax2 = axs
    

instead of `gridspec.GridSpec` to create the axes. It will have better looking
spacing between the axes "out-of-the-box".

