Act fast, special offers end soon! Up to $294 is savings when you get Premium today.
Get offer codes

How to plot a dictionary?

I want to uswplot.kde() to the dictionary to see if distrubution_1 is right skew or left skew. How to make it happen? thank you!!

Screen Link: https://app.dataquest.io/m/307/the-mode/6/skewed-distributions

My Code:

distribution_1 = {'mean': 3021 , 'median': 3001, 'mode': 2947}
distribution_2 = {'median': 924 , 'mode': 832, 'mean': 962}
distribution_3 = {'mode': 202, 'mean': 143, 'median': 199}

distribution_1.plot.kde()

What actually happened:

AttributeError: 'dict' object has no attribute 'plot'

@candiceliu93

I do not think this plot will show the skewness if it isn’t superimposed ontop of another plot. For this case, they plotted the houses['SalePrice'] kde first. The mean, median and mode plots were made ontop of this kde to see the skewness.

However, I think I know what your intentions were and I got the following graph when I followed the approach. Please note that kde works for a dataframe, not a dictionary. So I have to convert from a dictionary to a dataframe.

distribution_1 = {'mean': 3021 , 'median': 3001, 'mode': 2947}
distribution_2 = {'median': 924 , 'mode': 832, 'mean': 962}
distribution_3 = {'mode': 202, 'mean': 143, 'median': 199}

df = {'distribution_1': [i for i in distribution_1.values()],
      'distribution_2': [i for i in distribution_2.values()],
      'distribution_3': [i for i in distribution_3.values()]}

import pandas as pd
import matplotlib.pyplot as plt

pd.DataFrame(df).plot.kde()

Got this
image

Hi @candiceliu93,

You can use this syntax:

ax = pd.Series(list(distribution_1.values())).plot.kde() 
1 Like

good to know that I can use list comprehension to change the dictionary to df. I am still not familiar with list comprehension. Could you explain it? I always to need to write a for loop first then change to list comprehension.

Thank you!!

1 Like

Thank you!!! it helps!!

2 Likes

You are welcome @candiceliu93