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.
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.
viridis as your default colormap; it is perceptually uniform, meaning it works well for all users, including those with color vision deficiencies.
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?