0

I have a data set like

import pandas as pd

compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google']
products = ['OS', 'Search', 'E-comm', 'E-comm', 'Social Media', 'OS']
average = [1.2,3.4,2.4,5.2,3.2,4.4]

df = pd.DataFrame({'company' : compnaies, 'product':products,
                    'average' : average})
df

   average  company     product
0   1.2    Microsoft    OS
1   3.4    Google       Search
2   2.4    Amazon       Search
3   5.2    Microsoft    Search
4   4.4    Google       OS

Now I want to plot in Python/MatPlotLib it like enter image description here

I am from R background and whatever I found here is converting the x axis in numeric index, then plot 2 numeric values and then name the group in hard coded way, Is there any standard way where I can plot the required graph.

Most of the places, I find the help like this code where we plot all numeric value with static group https://matplotlib.org/examples/api/barchart_demo.html

2
  • Just had a similar question - see second version here Commented Jul 27, 2018 at 9:41
  • 1
    thanks, it works for me so I am closing this ticket Commented Jul 27, 2018 at 9:46

1 Answer 1

0

Closing this thread as this solution works for me

from matplotlib import pyplot as plt
import pandas as pd

    #create toy dataframe
    np.random.seed(1234)
    df = pd.DataFrame({"clm1": ["X", "Y", "Z", "X", "Y", "Z"], "clm2": np.random.randint(1, 100, 6), "clm3": ["A", "A", "A", "B", "B", "B"]})

    #rearrange dataframe and plot
    df.pivot(index = "clm3", columns = "clm1", values = "clm2").plot.bar(edgecolor = "white")
    plt.xticks(rotation = 0)
    plt.show()

taken from : python plot grouped bar graph

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.