Search code examples
pandasmatplotlibggbiplot

Is there a simple way to plot a multiple bar graph using iplot?


I have a dataset that looks like this, and I need to use the location as the color, the words as the x axis.

country   good   amazing    best
    Aus     12        45      12
   Fiji     25         5      23
    USA     45         5      12
     UK     88       258      18

Ideally, it would look like this:

enter image description here

I tried the following codes:

positive.iplot(kind = 'bar', title = 'Frequency of Positive Words per Country', y = 'Location', x = ['good', 'amazing', 'best'])

Solution

  • To generate a grouped barchart like the one you want, each country and its values should have its own column, so you'll need to transpose your df at at some point.

    Also in your example image, the height of the bars doesn't seem to match the values in your df, but I'll assume that's just an image to show the type of barchart you want to create.

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({
        'country': ["Aus","Fiji","USA","UK"], 
        'good': [12,25,45,88], 
        'amazing': [45,5,5,258],
        'best':[12,23,12,18]})
    
    df_values = df[['good','amazing','best']]
    df_values.index = df['country']
    
    # transpose the dataframe so that each country and its values have its own column
    ax = df_values.T.plot.bar(rot=0)
    plt.show()
    

    enter image description here