Create a bar chart in python but seperate column by a key

Question:

I have a dataframe that looks like this

d = {'total_time':[1,2,3,4],
    'date': ['2022-09-11', '2022-09-11', '2022-09-13', '2022-09-13'],
    'key': ['A', 'B', 'A', 'B']}
df_sample = pd.DataFrame(data=d)
df_sample.head()

I want to compare total_time across the data that should be in the x-axis but I want to compare these values by the associated ‘key’.

Therefore I should have something that looks like this

enter image description here

Asked By: Wolfy

||

Answers:

I adapted this example to your sample dataframe:

import numpy as np 
import matplotlib.pyplot as plt 
  
X_axis = np.arange(len(df_sample.date.unique()))
  
plt.bar(X_axis - 0.2, df_sample[df_sample.date=='2022-09-11']['total_time'], 0.4, label = '2022-09-11')
plt.bar(X_axis + 0.2, df_sample[df_sample.date=='2022-09-13']['total_time'], 0.4, label = '2022-09-13')
  

plt.xlabel("Key")
plt.ylabel("Total time")
plt.title("Total time per key")
plt.legend()
plt.show()

This snippet returns:

enter image description here

Answered By: Sheldon

You can use seaborn and pass the key column to hue argument :

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb


d = {'total_time':[1,2,3,4],
    'date': ['2022-09-11', '2022-09-11', '2022-09-13', '2022-09-13'],
    'key': ['A', 'B', 'A', 'B']}
df_sample = pd.DataFrame(data=d)

plt.figure()
sb.barplot(data = df_sample, x = 'date', y = 'total_time', hue = 'key')
plt.show()

enter image description here

See seaborn documentation : https://seaborn.pydata.org/generated/seaborn.barplot.html#seaborn.barplot

Answered By: Adrien Riaux
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.