Density plots
In practice, histograms can be a substandard method for assessing the distribution of your data because they can be strongly affected by the number of bins that have been specified. Instead, kernel density plots represent a more effective way to view the distribution of your data. An example of how to generate a density plot of is shown below:
ax = df.plot(kind='density', linewidth=2)
The standard .plot()
method is specified with the kind
argument set to 'density'
. We also specified an additional parameter linewidth
, which controls the width of the line to be plotted.
This exercise is part of the course
Visualizing Time Series Data in Python
Exercise instructions
- Using the
co2_levels
DataFrame, produce a density plot of the CO2 level data with line width parameter of 4. - Annotate the x-axis labels of your boxplot with the string
'CO2'
. - Annotate the y-axis labels of your boxplot with the string
'Density plot of CO2 levels in Maui Hawaii'
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Display density plot of CO2 levels values
ax = ____.____(____=____, ____=____, fontsize=6)
# Annotate x-axis labels
____.____('CO2', fontsize=10)
# Annotate y-axis labels
____.____('Density plot of CO2 levels in Maui Hawaii', fontsize=10)
plt.show()