Plot Python Dictionary using Multiple ways

Plot Python Dictionary using Multiple ways

We plot a python dictionary using python data visualization libraries like pandas, matplotlib, and seaborn. Actually, each python dictionary contains key and value pair. We can simply plot bar chart using the keys and values of the dictionary. All we need to put attention on data conversion for the required function in drawing bar charts. In this article, we will understand all these conversions and plotting with examples. so let’s start.

Plot python dictionary ( Methods ) –

But to start this we need to first create the sample dictionary which we will use as an example to plot.

sample_dict ={ "key1":10, "key2": 15, "key3": 13 }

Method 1: Pandas for plotting dictionary –

Here we need to convert the dict values in the list object and then we need to convert the dict to the pandas dataframe object. then we can use the below code for plotting.

import pandas as pd
sample_dict ={ "key1":[10], "key2": [15], "key3": [13]}
df = pd.DataFrame.from_dict(sample_dict)
df.plot(kind = 'bar')
plt.show()
Dictionary plottiing using pandas
Dictionary plottiing using pandas

Method 2: Matplotlib for plotting dictionary –

Here we have used the same sample dictionary and the type of visualization is also the same as the bar chart. Only in the place of the pandas library, we will use the matplotlib library and here we go.

import matplotlib.pyplot as plt
sample_dict ={ "key1":10, "key2": 15, "key3": 13 }
keys = list(sample_dict.keys())
values = list(sample_dict.values())

plt.bar(range(len(sample_dict)), values, tick_label=keys)
plt.show()

From the data point of view, we have extracted the keys and values for the dict and provided them into plt.bar chart. Here we do need not convert dict object to pandas dataframe. this is pretty simple and straightforward.

Dictionary plottiing using matplotlib
Dictionary plottiing using matplotlib

Method 3: Seaborn for plotting dictionary –

It is quite similar to the above two methods. Here we will use seaborn visualization library which sits on top of pandas and matplotlib both and here is the code. Here we will convert the dictionary into pandas first and then pass the same into sns.barplot() function. It will draw the bar chart on the basis of the values of the python dictionary and keys.

import seaborn as sns
import matplotlib.pyplot as plt
sample_dict ={ "key1":[10], "key2": [15], "key3": [13]}
df = pd.DataFrame.from_dict(sample_dict)
sns.barplot( data = df)
plt.show()
Dictionary plottiing using seaborn
Dictionary plottiing using seaborn

 

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner