Unit II lecturer notes
Unit II lecturer notes
IMPORTING MATPLOTLIB
To begin using Matplotlib in Python, the first step is importing it into your Python environment.
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in
Python. The core module for creating visualizations is pyplot, and it is typically imported for plotting.
python
Copy code
pyplot is a sub-module within matplotlib that provides a MATLAB-like interface for creating plots and
charts.
plt is an alias often used for matplotlib.pyplot, making it shorter and more convenient to reference.
Importing entire Matplotlib: If you need access to all the sub-modules of Matplotlib, you can import the
whole library.
python
Copy code
import matplotlib
This allows you to access not only pyplot but also other features like matplotlib.colors,
matplotlib.animation, etc.
Importing specific modules or classes: If you only need certain features from Matplotlib, you can import
them directly.
python
Copy code
python
Copy code
Setting up inline plots in Jupyter Notebooks: If you're working in a Jupyter Notebook environment and
want plots to display inline (i.e., directly below the code), use this magic command at the start of your
notebook:
python
Copy code
%matplotlib inline
This command ensures that figures and plots are displayed inside the notebook itself, rather than in
separate windows.
Importing for Interactive Environments (Tkinter or Qt): If you're working in an interactive environment
(e.g., Tkinter or PyQt), you can import Matplotlib's interactive backends to manage how figures are
displayed.
python
Copy code
Installation of Matplotlib
Before importing Matplotlib, it must be installed. You can install it using pip:
bash
Copy code
Copy code
Once Matplotlib is imported, you can start plotting. Here is an example of how you might create a
simple plot:
python
Copy code
# Example Data
x = [1, 2, 3, 4, 5]
plt.plot(x, y)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Simple Plot')
plt.show()
plt.plot(x, y): Plots the data x vs y. This creates a line plot by default.
plt.xlabel('X Axis') and plt.ylabel('Y Axis'): Set the labels for the x and y axes.
plt.title('Simple Plot'): Sets a title for the plot.
Customizing Plots
Matplotlib is highly customizable. You can change the line style, color, markers, and much more. Here's
a brief example:
python
Copy code
x = [1, 2, 3, 4, 5]
# Customizing the plot with a red dashed line and circle markers
plt.plot(x, y, 'r--o') # 'r' for red, '--' for dashed line, 'o' for circle markers
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Customized Plot')
plt.show()
Matplotlib Sub-modules
matplotlib.pyplot: Contains functions for creating various types of plots (line plots, bar plots, scatter
plots, histograms, etc.).
matplotlib.colors: Provides functions for handling color maps and color normalization.
matplotlib.figure: Used to create and manage figures (the entire window or image where plots are
drawn).
matplotlib.ticker: Useful for controlling tick formatting (e.g., on the x and y axes).
matplotlib.backends: Contains backends for displaying plots in different environments (e.g., Tkinter, Qt,
etc.).
In matplotlib, a simple line plot is a basic graph that displays data points as a series of connected line
segments. It is commonly used to visualize trends or patterns over a continuous range, such as time or
ordered categories.
python
Prepare data: You need two sets of data: one for the x-axis (independent variable) and one for the y-axis
(dependent variable).
Example data:
python
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
Plot the data: Use the plt.plot() function to create a line plot.
python
plt.plot(x, y)
Add labels and title: You can enhance your plot by adding a title, and labels for the x and y axes.
python
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
python
plt.show()
Full Example:
python
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Line Style:
Example:
python
Line Color:
You can set the color using a string (e.g., 'r' for red, 'g' for green, etc.).
Example:
python
Line Width:
Example:
python
Markers:
You can add markers at each data point using the marker parameter.
Example:
python
python
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
VISUALIZING ERRORS
Visualizing errors in data using Matplotlib is an essential technique, especially when presenting the
uncertainty or variability of measurements. Matplotlib provides several ways to represent errors in the
form of error bars, shaded regions, or confidence intervals, which help convey the uncertainty or
potential range of data points.
1. Error Bars
Error bars show the uncertainty in the measurements along the x, y, or both axes. You can add error
bars in Matplotlib using the errorbar function.
python
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.show()
xerr and yerr represent the error on the x-axis and y-axis, respectively.
fmt is the format for the data points (e.g., 'o' for circles).
capsize controls the size of the caps at the end of the error bars.
If the errors are asymmetric (i.e., they have different magnitudes in the positive and negative
directions), you can provide different values for the upper and lower errors.
python
xerr = [0.1, 0.2, (0.1, 0.3), 0.3, (0.2, 0.4)] # Asymmetric error on 3rd point
yerr = [0.2, (0.3, 0.5), 0.2, 0.3, (0.4, 0.6)] # Asymmetric error on 2nd and 5th points
plt.show()
Another way to visualize errors or uncertainty is to plot a shaded region, which can represent things like
confidence intervals or error bands.
python
import numpy as np
y = np.sin(x)
plt.legend()
plt.show()
In this example:
fill_between creates a shaded region between y - y_err and y + y_err to indicate the uncertainty range.
You can combine error bars with line plots to visualize errors at various points of the data.
python
y = np.cos(x)
plt.plot(x, y, label='cos(x)')
plt.legend()
plt.show()
Sometimes, you may want to show multiple types of errors, such as different error bars for different
sources of uncertainty. For example, if you have random error and systematic error:
python
y = np.exp(-0.1 * x)
systematic_error = 0.2
plt.show()
A density plot is a smoothed version of a histogram, providing a continuous estimate of the probability
density function of a random variable. It helps to visualize the distribution of data points in a more
continuous manner.
Usage: You typically use a density plot when you want to show the distribution of data and identify
patterns such as multi-modal distributions, peaks, and the spread of data.
How to create: In Matplotlib, a density plot is often created using seaborn.kdeplot() or matplotlib's
hist2d() function with density normalization.
Example:
python
import numpy as np
data = np.random.randn(1000)
sns.kdeplot(data, shade=True)
plt.title("Density Plot")
plt.show()
In the above example, seaborn.kdeplot generates a smooth curve that represents the estimated density
of the data.
2. Contour Plots
A contour plot is used to represent three-dimensional data in two dimensions, where contour lines or
filled regions represent constant values of a function over the data domain. It is useful for understanding
the structure of the data and detecting areas with higher or lower values.
Usage: Contour plots are used in situations where you want to visualize how a value varies across two
variables (typically spatial data or other 2D measurements), for example, in meteorology to show
altitude lines or in data science for visualizing decision boundaries in machine learning.
How to create: In Matplotlib, the function plt.contour() is used to create contour lines, and
plt.contourf() is used for filled contour plots.
Example:
python
import numpy as np
X, Y = np.meshgrid(x, y)
plt.title("Contour Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
If you want filled contour regions instead of lines, you can use plt.contourf().
Example with filled contour:
python
plt.colorbar()
plt.show()
Density Plots:
Typically represent the distribution of a single variable (1D data), or in the case of a 2D density plot, the
density of points in a 2D space.
Contour Plots:
Represent the relationship between two variables in 2D space, showing how a function or quantity
changes across that space.
Show contours (lines or filled regions) that indicate constant values over the plane.
When you're creating histograms in Matplotlib, a powerful plotting library in Python, the concepts of
histogram, legends, and colors are important for effectively visualizing and distinguishing the data.
1. Histogram
A histogram is a graphical representation of the distribution of numerical data. It’s constructed by
dividing the data into bins (intervals) and counting the number of data points that fall into each bin.
In Matplotlib, you can add legends using plt.legend(). Each plot element (like a line or a histogram) can
be given a label, which can then be included in the legend.
Example:
python
import matplotlib.pyplot as plt
import numpy as np
data1 = np.random.randn(1000)
data2 = np.random.randn(1000)
# Plot two histograms with legends
plt.hist(data1, bins=30, alpha=0.5, label='Data 1')
plt.hist(data2, bins=30, alpha=0.5, label='Data 2')
# Adding legend
plt.legend()
# Adding legend
plt.legend()
plt.title('Histogram with Custom Colors')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
4. Coloring Bars Based on Value (Colormaps)
If you want to color the bars based on their height or frequency, you can use a colormap. Matplotlib
supports several built-in colormaps, which can be applied to your histogram.
For example, you can map the height of the bars to a color scale using the cmap argument. To do this,
you would typically calculate the histogram, then apply the colormap to each bar based on its height.
Subplots in Matplotlib
When visualizing data using Matplotlib, you often need to create multiple plots in a single figure to
compare trends or analyze different aspects of the data. Matplotlib's subplots feature provides a simple
way to create and manage multiple plots within the same figure.
Basic Concepts:
A figure is the overall window or page on which the plots appear.
A subplot is one plot among many on a figure.
The subplots function allows you to create a grid of subplots, and you can specify the number of rows
and columns.
1. Basic Syntax of plt.subplots()
python
Copy code
import matplotlib.pyplot as plt
# Plotting example
ax[0, 0].plot([1, 2, 3], [4, 5, 6])
ax[0, 1].plot([1, 2, 3], [6, 5, 4])
ax[1, 0].plot([1, 2, 3], [7, 8, 9])
ax[1, 1].plot([1, 2, 3], [9, 8, 7])
plt.show()
Explanation:
nrows and ncols define the number of rows and columns for subplots, respectively.
ax is a 2D array of Axes objects where you can access individual subplots by indexing them (e.g., ax[0,
0]).
2. Tight Layout
The tight_layout() function automatically adjusts subplot parameters to give some padding between
plots and avoid overlapping.
python
Copy code
fig, ax = plt.subplots(2, 2)
plt.tight_layout()
plt.show()
3. Sharing Axes in Subplots
Sometimes it is useful to have subplots that share the same x-axis or y-axis for easy comparison. You can
use the sharex and sharey options.
python
Copy code
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
# Plotting example
ax[0, 0].plot([1, 2, 3], [4, 5, 6])
ax[0, 1].plot([1, 2, 3], [6, 5, 4])
ax[1, 0].plot([1, 2, 3], [7, 8, 9])
ax[1, 1].plot([1, 2, 3], [9, 8, 7])
plt.tight_layout()
plt.show()
Explanation:
python
Copy code
fig, ax = plt.subplots(2, 2, figsize=(8, 6))
plt.show()
Explanation:
figsize=(width, height) allows you to control the size of the entire figure in inches.
5. Handling Subplots in a Loop
You can also create subplots in a loop when working with dynamic data or a large number of plots.
python
Copy code
fig, ax = plt.subplots(2, 2)
for i in range(2):
for j in range(2):
ax[i, j].plot([1, 2, 3], [4 * i + j, 5 * i + j, 6 * i + j])
plt.tight_layout()
plt.show()
6. 1xN and Nx1 Subplots (Single Row or Single Column)
For linear arrangements of subplots, such as all plots in one row or one column, plt.subplots() makes it
easy to manage these cases.
plt.tight_layout()
plt.show()
Example for a 3x1 subplot layout:
python
Copy code
fig, ax = plt.subplots(3, 1) # 3 rows, 1 column
ax[0].plot([1, 2, 3], [1, 2, 3])
ax[1].plot([1, 2, 3], [4, 5, 6])
ax[2].plot([1, 2, 3], [7, 8, 9])
plt.tight_layout()
plt.show()
7. Removing Unused Subplots
If you create a grid of subplots but don't need all of them, you can hide unused subplots.
python
Copy code
fig, ax = plt.subplots(2, 2)
python
Copy code
import matplotlib.gridspec as gridspec
fig = plt.figure()
gs = gridspec.GridSpec(3, 3)
plt.tight_layout()
plt.show()
9. Adding Titles, Labels, and Legends to Subplots
You can add titles and labels to individual subplots for better clarity:
python
Copy code
fig, ax = plt.subplots(2, 2)
plt.tight_layout()
plt.show()
10. Saving Figures with Subplots
To save a figure with subplots, use the savefig() function.
python
Copy code
fig, ax = plt.subplots(2, 2)
plt.savefig('my_subplots_figure.png')
Conclusion:
The subplots function in Matplotlib is a powerful tool for creating complex visualizations with multiple
plots. Using features like sharex, sharey, tight_layout(), and GridSpec, you can customize and arrange
your plots efficiently.
Basic Syntax:
python
Copy code
plt.text(x, y, s, fontsize=12, color='black')
x, y: The coordinates where the text will appear.
s: The text string itself.
fontsize: (optional) Controls the font size of the text.
color: (optional) Specifies the color of the text.
Example:
python
Copy code
import matplotlib.pyplot as plt
# Sample plot
plt.plot([1, 2, 3], [4, 5, 6])
# Add text
plt.text(2, 5, 'Data point (2, 5)', fontsize=12, color='red')
plt.show()
2. Adding Annotations in Matplotlib
The annotate() function allows you to add text annotations that are connected to a specific data point.
Basic Syntax:
python
Copy code
plt.annotate(text, xy, xytext=None, arrowprops=None)
text: The annotation text.
xy: The coordinates of the point you want to annotate (where the arrow will point).
xytext: (optional) The position of the text.
arrowprops: (optional) A dictionary of properties to customize the arrow.
Example with Arrow:
python
Copy code
import matplotlib.pyplot as plt
# Sample plot
plt.plot([1, 2, 3], [4, 5, 6])
# Add annotation
plt.annotate('Important point', xy=(2, 5), xytext=(3, 4),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
Customization Options for Annotations
Text properties: You can change font size, color, style, etc.
Arrow properties: You can customize the arrow appearance with properties like arrowstyle, color,
linewidth, etc.
Example of Custom Arrow:
python
Copy code
plt.annotate('Critical point', xy=(2, 5), xytext=(3, 4),
arrowprops=dict(facecolor='blue', arrowstyle='->', linewidth=2))
3. Text Box (Bounding Box)
Sometimes, you want to place text inside a box for better visibility.
python
Copy code
plt.text(2, 5, 'Data point (2, 5)', fontsize=12, bbox=dict(facecolor='yellow', alpha=0.5))
In this example, the bbox argument creates a semi-transparent yellow background behind the text.
Complete Example:
python
Copy code
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)
plt.show()
This shows how you can use text and annotations to make your visualizations more informative and
aesthetically appealing.
Customization
Figure size: You can change the figure size using figsize.
python
Copy code
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6)) # Width: 10, Height: 6
Multiple subplots: Use plt.subplot() or plt.subplots() for more control over
multiple plots.
python
Copy code
fig, ax = plt.subplots(2, 2, figsize=(10, 8)) # 2x2 grid of plots
2. Line and Marker Styles
Line styles: Customize line properties like color, width, and style using parameters like
color, linewidth, and linestyle.
python
Copy code
plt.plot(x, y, color='red', linewidth=2, linestyle='--')
Markers: Change marker style with the marker, markersize, and markerfacecolor
parameters.
python
Copy code
plt.plot(x, y, marker='o', markersize=10, markerfacecolor='blue')
3. Axes Customization
Limits: You can set custom limits on x and y axes with plt.xlim() and plt.ylim().
python
Copy code
plt.xlim([0, 10])
plt.ylim([0, 100])
Ticks: Customize tick marks using plt.xticks() and plt.yticks(), where you can
control locations and labels.
python
Copy code
plt.xticks([0, 2, 4, 6, 8, 10], ['A', 'B', 'C', 'D', 'E', 'F'])
4. Labels and Titles
Title and labels: Add a title, x-axis, and y-axis labels using plt.title(),
plt.xlabel(), and plt.ylabel().
python
Copy code
plt.title('Sample Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
Font properties: Customize the font style, size, and weight for titles and labels.
python
Copy code
plt.title('Sample Plot', fontsize=16, fontweight='bold',
fontfamily='serif')
5. Legends
python
Copy code
plt.plot(x, y, label='Data 1')
plt.legend(loc='upper left')
Legend customization: You can control the location, frame, and font size of the legend.
python
Copy code
plt.legend(loc='best', fontsize=12, frameon=False)
6. Gridlines
python
Copy code
plt.grid(True, linestyle='--', color='gray', alpha=0.7)
7. Annotations
Text annotations: Use plt.text() or plt.annotate() to place custom text on the plot.
python
Copy code
plt.annotate('Important Point', xy=(5, 50), xytext=(6, 60),
arrowprops=dict(facecolor='black', arrowstyle='->'))
8. Colormaps and Colorbars
Colormap: For heatmaps or other color-based plots, you can use built-in colormaps.
python
Copy code
plt.imshow(data, cmap='viridis')
Colorbar: Add a colorbar to explain color scales.
python
Copy code
plt.colorbar()
9. Stylesheets
python
Copy code
plt.style.use('ggplot')
10. Saving Figures
python
Copy code
plt.savefig('plot.png', dpi=300, bbox_inches='tight')
Example Putting it All Together
python
Copy code
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
plt.plot(x, y, label='sin(x)', color='blue', linewidth=2, linestyle='--',
marker='o')
plt.xlim(0, 10)
plt.ylim(-1.5, 1.5)
plt.show()
# Data generation
x = np.linspace(-5, 5, 100)
y = np.sin(x)
z = np.cos(x)
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()
Example 2: 3D Surface Plot
A surface plot is used to visualize a 3D surface.
python
Copy code
# Data generation
X = np.linspace(-5, 5, 100)
Y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(X, Y)
Z = np.sin(np.sqrt(X**2 + Y**2))
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()
Example 3: 3D Scatter Plot
python
Copy code
# Data generation
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()
Customization
You can customize colors, labels, title, and viewing angles using:
ax.view_init(elev, azim) – sets the elevation and azimuth angles of the plot.
ax.set_title('Title') – adds a title to the plot.
ax.set_xlim([xmin, xmax]), ax.set_ylim([ymin, ymax]), ax.set_zlim([zmin, zmax]) – to control the axes
limits.
python
Copy code
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
python
Copy code
import numpy as np
# Convert latitudes and longitudes to x and y coordinates using the Basemap instance
x, y = m(lons, lats)
Projection: There are many projections available such as cyl (Cylindrical Equidistant), merc (Mercator),
ortho (Orthographic), etc.
Resolution: The resolution of coastline data can be 'c' (crude), 'l' (low), 'i' (intermediate), 'h' (high), and 'f'
(full).
Shading: You can use functions like m.bluemarble(), m.shadedrelief(), or m.etopo() to display shaded
relief or topography.
For example:
python
Copy code
# Shaded relief map with a different projection (Mercator)
m = Basemap(projection='merc',
llcrnrlat=-60, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180,
resolution='l')
python
Copy code
# Assuming you have a shapefile of countries
m.readshapefile('shapefiles/countries', 'countries')
Seaborn is a powerful data visualization library built on top of Matplotlib. It provides an interface for
creating informative and attractive statistical graphics. While Matplotlib is more versatile and gives
greater control over individual elements, Seaborn simplifies common tasks and produces aesthetically
pleasing default plots.
python
Copy code
# Load an example dataset
tips = sns.load_dataset("tips")
python
Copy code
plt.figure(figsize=(8, 6)) # Adjust the figure size with Matplotlib
sns.scatterplot(x="total_bill", y="tip", data=tips)
plt.show()
Here, we used plt.figure() to adjust the size of the plot and then added titles and labels using
Matplotlib.
python
Copy code
plt.figure(figsize=(8, 6))
sns.histplot(tips['total_bill'], bins=20, kde=True) # kde adds a smooth line
plt.title("Distribution of Total Bill")
plt.xlabel("Total Bill")
plt.ylabel("Frequency")
plt.show()
Box Plot (sns.boxplot): Useful for visualizing the spread and outliers in a dataset.
python
Copy code
plt.figure(figsize=(8, 6))
sns.boxplot(x="day", y="total_bill", data=tips)
plt.title("Box Plot of Total Bill by Day")
plt.show()
Heatmap (sns.heatmap): Often used to visualize correlation matrices or other grid data.
python
Copy code
plt.figure(figsize=(8, 6))
corr = tips.corr()
sns.heatmap(corr, annot=True, cmap="coolwarm")
plt.title("Correlation Heatmap of Tips Dataset")
plt.show()
Overlaying Seaborn and Matplotlib
You can overlay Matplotlib and Seaborn plots:
python
Copy code
plt.figure(figsize=(8, 6))
# Seaborn plot
sns.histplot(tips['total_bill'], bins=20, color='blue', label="Seaborn")
# Add legend
plt.legend()
By combining the simplicity of Seaborn with the fine-tuning controls of Matplotlib, you can create
sophisticated and visually appealing visualizations for your data analysis.