Matplotlib Tutorial for Data Visualization
Dar es salaam Maritime Institute
1 Introduction to Matplotlib
Matplotlib is a powerful Python library for creating static, animated, and interactive visualizations.
The ‘pyplot‘ module provides a MATLAB-like interface for plotting graphs. This tutorial will guide
you through the basics of Matplotlib, starting with simple examples and building up to the code you
provided.
2 Installing and Importing Matplotlib
Before using Matplotlib, you need to install it. You can install it using pip:
1 pip install matplotlib
To import Matplotlib in your Python script:
1 import matplotlib . pyplot as plt
3 Basic Matplotlib Examples
3.1 Plotting a Simple Line Graph
The simplest way to create a plot is by using the ‘plot‘ function.
1 # Data
2 x = [1 , 2 , 3 , 4 , 5]
3 y = [2 , 4 , 6 , 8 , 10]
4
5 # Create a plot
6 plt . plot (x , y )
7
8 # Add labels and title
9 plt . xlabel ( ’X - axis ’)
10 plt . ylabel ( ’Y - axis ’)
11 plt . title ( ’ Simple Line Graph ’)
12
13 # Display the plot
14 plt . show ()
3.2 Plotting Multiple Lines
You can plot multiple lines on the same graph by calling ‘plot‘ multiple times.
1 # Data
2 x = [1 , 2 , 3 , 4 , 5]
3 y1 = [2 , 4 , 6 , 8 , 10]
4 y2 = [1 , 3 , 5 , 7 , 9]
5
6 # Create plots
7 plt . plot (x , y1 , label = ’ Line 1 ’)
8 plt . plot (x , y2 , label = ’ Line 2 ’)
9
10 # Add labels , title , and legend
11 plt . xlabel ( ’X - axis ’)
12 plt . ylabel ( ’Y - axis ’)
13 plt . title ( ’ Multiple Lines ’)
1
14 plt . legend ()
15
16 # Display the plot
17 plt . show ()
3.3 Customizing Line Styles and Markers
You can customize the appearance of lines and markers using additional arguments in the ‘plot‘ function.
1 # Data
2 x = [1 , 2 , 3 , 4 , 5]
3 y = [2 , 4 , 6 , 8 , 10]
4
5 # Create a plot with custom style
6 plt . plot (x , y , linestyle = ’ -- ’ , marker = ’o ’ , color = ’r ’ , label = ’ Custom Line
’)
7
8 # Add labels , title , and legend
9 plt . xlabel ( ’X - axis ’)
10 plt . ylabel ( ’Y - axis ’)
11 plt . title ( ’ Customized Line Graph ’)
12 plt . legend ()
13
14 # Display the plot
15 plt . show ()
4 Intermediate Matplotlib Examples
4.1 Scatter Plot
Scatter plots are useful for visualizing relationships between two variables.
1 # Data
2 x = [1 , 2 , 3 , 4 , 5]
3 y = [2 , 4 , 6 , 8 , 10]
4
5 # Create a scatter plot
6 plt . scatter (x , y , color = ’b ’ , label = ’ Data Points ’)
7
8 # Add labels , title , and legend
9 plt . xlabel ( ’X - axis ’)
10 plt . ylabel ( ’Y - axis ’)
11 plt . title ( ’ Scatter Plot ’)
12 plt . legend ()
13
14 # Display the plot
15 plt . show ()
4.2 Bar Plot
Bar plots are useful for comparing categorical data.
1 # Data
2 categories = [ ’A ’ , ’B ’ , ’C ’ , ’D ’]
3 values = [10 , 20 , 15 , 25]
4
5 # Create a bar plot
6 plt . bar ( categories , values , color = ’g ’ , label = ’ Values ’)
7
8 # Add labels , title , and legend
9 plt . xlabel ( ’ Categories ’)
10 plt . ylabel ( ’ Values ’)
11 plt . title ( ’ Bar Plot ’)
12 plt . legend ()
13
14 # Display the plot
15 plt . show ()
2
4.3 Histogram
Histograms are useful for visualizing the distribution of data.
1 # Data
2 data = [1 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 5 , 5 , 5 , 5 , 5]
3
4 # Create a histogram
5 plt . hist ( data , bins =5 , color = ’m ’ , edgecolor = ’ black ’ , label = ’ Data
Distribution ’)
6
7 # Add labels , title , and legend
8 plt . xlabel ( ’ Value ’)
9 plt . ylabel ( ’ Frequency ’)
10 plt . title ( ’ Histogram ’)
11 plt . legend ()
12
13 # Display the plot
14 plt . show ()
5 Advanced Matplotlib Examples
5.1 Subplots
Subplots allow you to create multiple plots in a single figure.
1 # Data
2 x = [1 , 2 , 3 , 4 , 5]
3 y1 = [2 , 4 , 6 , 8 , 10]
4 y2 = [1 , 3 , 5 , 7 , 9]
5
6 # Create subplots
7 fig , ( ax1 , ax2 ) = plt . subplots (1 , 2 , figsize =(10 , 4) )
8
9 # Plot on the first subplot
10 ax1 . plot (x , y1 , color = ’r ’ , label = ’ Line 1 ’)
11 ax1 . set_xlabel ( ’X - axis ’)
12 ax1 . set_ylabel ( ’Y - axis ’)
13 ax1 . set_title ( ’ Subplot 1 ’)
14 ax1 . legend ()
15
16 # Plot on the second subplot
17 ax2 . plot (x , y2 , color = ’b ’ , label = ’ Line 2 ’)
18 ax2 . set_xlabel ( ’X - axis ’)
19 ax2 . set_ylabel ( ’Y - axis ’)
20 ax2 . set_title ( ’ Subplot 2 ’)
21 ax2 . legend ()
22
23 # Display the plots
24 plt . show ()
5.2 Using Matplotlib in the Provided Code
The provided code uses Matplotlib to visualize oil production data and the fitted curve. Let’s break it
down step by step.
5.2.1 Plotting the Data
The actual production data is plotted as blue circles.
1 # Plot the data
2 plt . plot ( data [ ’ Months ’] , data [ ’ Production ’] , ’ bo ’ , label = ’ Actual
Production ’)
3 plt . xlabel ( ’ Months ’)
4 plt . ylabel ( ’ Production ’)
5 plt . title ( ’ Production Over Time ’)
6 plt . legend ()
7 plt . show ()
3
5.2.2 Plotting the Fitted Curve
The fitted curve is plotted as a red line on top of the actual data.
1 # Plot the results
2 plt . figure ( figsize =(10 , 6) )
3 plt . plot ( data_cleaned [ ’ Months ’] , data_cleaned [ ’ Production ’] , ’ bo ’ , label
= ’ Actual Production ’)
4 plt . plot ( data_cleaned [ ’ Months ’] , result . best_fit , ’r - ’ , label = ’ Fitted
Curve ’)
5 plt . xlabel ( ’ Months ’)
6 plt . ylabel ( ’ Production ’)
7 plt . legend ()
8 plt . title ( ’ DCA Using Exponential Model ’)
9 plt . grid ( True )
10 plt . show ()
6 Conclusion
Matplotlib is a versatile library for creating a wide range of visualizations. This tutorial covered the
basics of plotting line graphs, scatter plots, bar plots, histograms, and subplots. By following these
examples, you should be able to understand and write the provided code for visualizing oil production
data and fitted curves.