Table of Contents
In this Python tutorial, learn to write a Python program to display a scatterplot using Matplotlib. Matplotlib is a Python module that will be used for plotting interactive graphs and plots for data visualization. If you are just starting to use Python or new to data science, visualization will be a very important step in your learning how how data is split.
What’s Matplotlib?
Matplotlib was introduced in 2002 by John Hunter. The Python library Matplotlib is a 2D plotting library that produces figures visually with large amounts of data. Matplotlib works with Numpy and SciPy to create a visualization with bar plots, line plots, scatterplots, histograms and much more.
Simple Matplotlib Plot
Matplotlib has multiple styles avaialble when trying to create a plot. If you are looking to confirm what styles are avilable for use, run the below command and import matplotlib.pyplot:
Input:
import matplotlib.pyplot as plt print(plt.style.available)
Output:
['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tableau-colorblind10', '_classic_test']
Matplotlib Composing Plot Styles
We can easily combine the passing of a list of styles that are designed to be composed together. For example, one style sheet can be customized for colors and another style sheet will alter element sizes for a presentation
plt.style.use(['Solarize_Light2', 'presentation'])
Matplotlib Scatterplot Functions
In this scatterplot example, we will only the matplotlib library to create the plot with the the
linspace() function
The linspace() function returns an evenly spaced numbers over a specified interval. In addition, it will return the number evenly spaced, which are then calculated over the interval, start and stop.
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
sin() function
The sin() function (sine) is used in trigonometry, which is the mathematical study of triangles. Sines of angles between and
are negative. The numerous properties of the sine and related functions are included in any standard trigonometry text.
scatter() function
The scatter() function is a built-in function in matplotlib. The scatter() function shows data as a collection of points and the position of a point will depends on it’s two-dimensional value. Each value is a position on either the horizontal or vertical dimension.
title(), ylabel() and xlaberl() functions
The title(), ylabel() and xlabel() functions are all function that define text on the scatterplot.
show() function
The show() function allows the scatterplot to display. This function starts an event loop and looks for current active figure objects, and opens one or more interactive windows that display your figure or figures.
Matplotlib line Scatterplot
Input:
from matplotlib import pyplot as plt x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] plt.plot(x, y) plt.show()
Output:
Simple Matplotlib Scatterplot Example
Input:
import matplotlib.pyplot as plt import numpy as np plt.style.use('seaborn-darkgrid') x = np.linspace(0, 40, 80) y = np.sin(x) plt.plot(x, y, 'o', color='green') plt.title('Basic Python Scatter Plot Style: seaborn-darkgrid') plt.show()
Output:
Matplotlib and NumPy Scatterplot
In the next scatterplot example, NumPy is used to generate a random number for the x-axis and y-axis to create the scatterplot.
Input:
import numpy as np import matplotlib.pyplot as plt # Create data n = 2000 x = np.random.rand(n) y = np.random.rand(n) colors = (0,0,0) area = np.pi*4 # Plot plt.scatter(x, y, s=area, alpha=0.5) plt.title('Basic Python Matplotlib with Numpy Random Scatterplot') plt.xlabel('x-axis') plt.ylabel('y-axis') plt.show()
Output: