Grouped Bar graph Pandas

Using pandas: import pandas as pd groups = [[23,135,3], [123,500,1]] group_labels = [‘views’, ‘orders’] # Convert data to pandas DataFrame. df = pd.DataFrame(groups, index=group_labels).T # Plot. pd.concat( [ df.mean().rename(‘average’), df.min().rename(‘min’), df.max().rename(‘max’) ], axis=1, ).plot.bar()

How to create a grouped bar plot

Pandas will show grouped bars by columns. Entries in each row but different columns will constitute a group in the resulting plot. Hence you need to “reshape” your dataframe to have the “group” as columns. In this case you can pivot like df.pivot(“column”, “group”, “val”) producing group g1 g2 column c1 10 8 c2 12 … Read more

Change bar color according to hue name

A. using a list of colors The easiest solution to make sure to have the same colors for the same categories in both plots would be to manually specify the colors at plot creation. # First bar plot ax = sns.barplot(data=a, x=’Scenario’, y=’Duration’, hue=”Program”, ci=None, palette=[“C0”, “C1”, “k”]) # … # Second bar plot ax2 … Read more

How to plot multiple bars grouped

import matplotlib.pyplot as plt from matplotlib.dates import date2num import datetime x = [ datetime.datetime(2011, 1, 4, 0, 0), datetime.datetime(2011, 1, 5, 0, 0), datetime.datetime(2011, 1, 6, 0, 0) ] x = date2num(x) y = [4, 9, 2] z = [1, 2, 3] k = [11, 12, 13] ax = plt.subplot(111) ax.bar(x-0.2, y, width=0.2, color=”b”, align=’center’) … Read more

How to add percentages on top of grouped bars

The seaborn.catplot organizing function returns a FacetGrid, which gives you access to the fig, the ax, and its patches. If you add the labels when nothing else has been plotted you know which bar-patches came from which variables. From @LordZsolt’s answer I picked up the order argument to catplot: I like making that explicit because … Read more

Plot multiple columns of pandas DataFrame on the bar chart

Tested in python 3.11, pandas 1.5.1, matplotlib 3.6.2 Sample Data and Imports import pandas as pd import matplotlib.pyplot as plt import numpy as np np.random.seed(2022) # creates a consistent sample y = np.random.rand(10,4) y[:,0]= np.arange(10) df = pd.DataFrame(y, columns=[“X”, “A”, “B”, “C”]) X A B C 0 0.0 0.499058 0.113384 0.049974 1 1.0 0.486988 0.897657 … Read more