1 Hour

4. Quantitative Distributions

Visualizing Distributions with Histograms

To understand the spread of a continuous variable, we utilize plt.hist. This function creates a histogram where the data is divided into bins. Adjusting the bins parameter is critical, as it changes the granularity of the visualization.

By setting alpha for transparency and edgecolor for clarity, you can create professional-grade distribution plots that effectively communicate frequency density.

Operator Note: Always experiment with different bin counts to ensure your histogram isn't hiding patterns due to over-smoothing or under-smoothing.
Python 3
import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)
plt.hist(data, bins=30, color='skyblue', edgecolor='black', alpha=0.7)
plt.title('Distribution Analysis')
plt.show()

Correlations and Colormaps

Scatter plots generated via plt.scatter are essential for observing the relationship between two quantitative variables. To add a third dimension, we can map data points to a color scale using the c parameter along with cmap.

Commonly used colormaps include viridis and plasma. Finally, plt.colorbar is used to provide a reference scale for the mapped colors, ensuring the visualization remains interpretable.

Operator Note: Use viridis as your default colormap; it is perceptually uniform, meaning it works well for all users, including those with color vision deficiencies.
Python 3
import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)

plt.scatter(x, y, c=colors, cmap='viridis', marker='o')
plt.colorbar(label='Intensity Scale')
plt.title('Variable Correlation Plot')
plt.show()

Knowledge Check

Ready to test your understanding of 4. Quantitative Distributions?

Back