Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot dictionary of dictionaries in one barplot

I've got a dictionary that looks like this

d = {
    'a' : {'L1':2, 'L2':5, 'L3':8},
    'b' : {'L1':4, 'L2':7, 'L3':10},
    'c' : {'L1':19, 'L2':0, 'L3':1},
}

I would like to have a plot where the x axis contains my keys and each key has 3 barplots that correspond to the values of L1, L2 and L3.
All in all, my plot would contain 9 bars grouped by the key (so 3 groups of 3 bars).

So far, what I could do was convert my dictionary to a dataframe and then use seaborn's barplot for each of the keys but that leaves my with 3 different plots.

Is it possible to have one plot with all the informations ?

Many thanks.

like image 492
mlx Avatar asked Oct 20 '25 13:10

mlx


1 Answers

You can use pandas, i.e:

import matplotlib.pyplot as plt
import pandas as pd

d = {
    'a': {'L1':2, 'L2':5, 'L3':8},
    'b': {'L1':4, 'L2':7, 'L3':10},
    'c': {'L1':19, 'L2':0, 'L3':1},
}
pd.DataFrame(d).plot(kind='bar')
plt.show()

Output:

output

In your case you need dict keys in x-axis so you can use

pd.DataFrame(d).T.plot(kind='bar')
like image 92
Bharath Avatar answered Oct 23 '25 03:10

Bharath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!