Visualizations with Matplotlib
A picture is worth a thousand words
What is Visualization?
A human mind can easily read and understand a chart or image as compared to looking through a large chunk of data in a table or a spreadsheet. Data visualization is a powerful technique to visualize and get meaningful insights from the dataset.
Matplotlib
Matplotlib provides a way to easily generate a wide variety of plots and charts in a few lines of Python code. It is an open source project that can be integrated into Python scripts, jupyter notebooks, web application servers, and multiple GUI toolkits.
Installing Matplotlib
The best way to install matplotlib is by downloading the Anaconda distribution. Anaconda is a set of python libraries which has the standard python programming language libraries as well as numerous third party scipy libraries like numpy, pandas, matplotlib etc.
Or you can install matplotlib by using pip
Plotting a simple graph.
Let’s start by plotting a simple graph.
Adding a legend
A proper figure is not complete without its own legend. Matplotlib provides a way to generate a legend with the minimal amount of effort.
Creating a Subplot
Sometimes it is helpful to compare different views of data side by side. To help with this, Matplotlib has the concept of Subplots: groups of smaller axes that can exist together within a single figure.
Customizing Plot Appearance
Scatter Plot
Scatter plots are used to plot data points on a horizontal and a vertical axis in the attempt to show how much one variable is affected by another. Each row in the data table is represented by a marker whose position depends on its values in the columns set on the X and Y axes.
Histogram
Histograms are useful for plotting the distribution of numbers across a range of possible values. It works by taking a list of numbers, binning those numbers within a number of ranges, and counting the number of occurrences in each bin.
Bar Chart
Bar charts are used to display values associated with categorical data.
Check out the matplotlib documentation to deep dive into this.
Also cheat sheet can be found here.