Data Visualization With Python

7. Histograms, Pie Charts, and Boxplots

In this lesson, you will learn how to create histograms, pie charts, and boxplots in Matplotlib.


Histograms show frequency distributions of data, meaning that you only need one array of values. We can use plt.hist() to generate a histogram of an array of numbers. Adjusting bar width and color can be done the same way as any other type of chart.

Pie Charts

Like histograms, pie charts also show distributions. To plot a pie chart, first create an array specifying the number of values in each category (i.e. [35, 25, 20, 15, 10, 5]). Using plt.pie(), a pie chart can be generated using this array.

To add labels, create an array of strings corresponding to each index in the frequency array. You can add the labels using the labels parameter. Colors can be changed by adding an array of strings specifying colors and calling this array in the color parameter. A legend can be created using the plt.legend() feature.


Boxplots are another way to show the distribution of numerical data. First, create an array containing all the arrays of data you want to plot. Then, use plt.boxplot() to create a boxplot of the data.