Introduction to Mathplotlib in Python
by K. Yue
1. Overview of Mathplotlib module
- "Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python."
- Resources:
- Main site: https://matplotlib.org/
- mathplotlib at w3schools: https://www.w3schools.com/python/matplotlib_intro.asp
- Codebase in Github: https://github.com/matplotlib/matplotlib
- Installation: at command line prompt: "pip install matplotlib"
- Two types of interfaces:
- User interfaces: a set of rules and protocols of a software program to communicate with its users.
- Application Programming Interface (API): a set of rules and protocols for two software programs to communicate with each other.
Example:
- mathplotlib module is a software program.
- A program, e.g., simple_plot_1.py, that use mathplotlib is another program.
- mathplotlib provides API for simple_plot_1.py to use its functionality.
2. Simple Mathplotlib programs
- Mathplotlib provides two main interfaces in its API:
- Pyplot API:
- It provides a collection of command-style functions that implicitly track the current figure and axes, making it work like MATLAB.
- It is generally simpler for quick, interactive plots and basic scripting.
- Object-Oriented (OO) API:
- It provides a collection of objects for greater control and customization.
- It is the recommended approach.
- Two main objects to start with:
- Figure: The top-level container for all plot elements. A figure may contain more than one plots.
- Axes: Represents a single plot area (e.g., an x-y graph) within the Figure and contains most of the plot elements like ticks, labels, and lines.
What is an object in Python?
In Python, an object is the fundamental abstraction for data for convenient organization and management, and virtually every "thing" in Python is an object.
Conceptually, an object can be understood as a self-contained entity that bundles together three main components:
- Attributes: constants or variables that define the object's structures, properties and internal states.
- Methods: functions defining the behavior of the object: what it can do or how it interacts with other objects.
- Identity: A unique identifier (which can be checked using the id() function) that distinguishes it from other objects in memory.
An object is a specific instance of a class, which acts as a blueprint or template for creating objects with a specific structure and behavior.
Example: Consider the string s = 'hello, world'
- s is an object of the class str.
- A string is an immutable sequence of Unicode characters. Attributes cannot be accessed directly.
- A string object (of the class str) has many methods: e.g., count, index, upper, lower, ...
|
Example:
simple_plot_1.py: run, study and annotate it.
import matplotlib.pyplot as plt
# Sample data: (x, y)
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Plot the data using pyplot.
plt.plot(x, y, marker='x') # 'o' for markers
# Add labels and title
plt.xlabel("Number")
plt.ylabel("Number squared")
plt.title("Square (Pyplot verson)")
# Display the plot
plt.show()
# Create a figure and axes objects explicitly
fig, ax = plt.subplots()
# Plot data using the ax.plot() method
ax.plot(x, y, marker='o')
# Set labels and title using axes methods
ax.set_xlabel("Number")
ax.set_ylabel("Number squared")
ax.set_title("Square (OO version)")
# Display the plot
plt.show()