Home > front end >  How to convert a vertical grouped bar plot into a horizontal grouped bar plot in python
How to convert a vertical grouped bar plot into a horizontal grouped bar plot in python

Time:05-10

I have a code in which I am plotting a graph in a vertical manner. What changes should I make in this code in order for the bar graph to be horizontal?

# importing package
import matplotlib.pyplot as plt
import pandas as pd
  
# create data
df = pd.DataFrame([['A', 10, 20, 10, 30], ['B', 20, 25, 15, 25], ['C', 12, 15, 19, 6],
                   ['D', 10, 29, 13, 19]],
                  columns=['Team', 'Round 1', 'Round 2', 'Round 3', 'Round 4'])
# view data
print(df)
  
# plot grouped bar chart
df.plot(x='Team',
        kind='bar',
        stacked=False,
        title='Grouped Bar Graph with dataframe')

CodePudding user response:

To change the existing vertical bar graph to horizontal, you only need to update the plot code as below

df.plot.barh(x='Team',
        stacked=False,
        title='Grouped Bar Graph with dataframe')

Output graph

Horizontal Bar Graph

  • Related