Home > Enterprise >  Create a bar chart in python but seperate column by a key
Create a bar chart in python but seperate column by a key

Time:11-01

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

CodePudding user response:

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 : enter image description here

  • Related