- Matplotlib Basics
- Matplotlib - Home
- Matplotlib - Introduction
- Matplotlib - Vs Seaborn
- Matplotlib - Environment Setup
- Matplotlib - Anaconda distribution
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - Simple Plot
- Matplotlib - Saving Figures
- Matplotlib - Markers
- Matplotlib - Figures
- Matplotlib - Styles
- Matplotlib - Legends
- Matplotlib - Colors
- Matplotlib - Colormaps
- Matplotlib - Colormap Normalization
- Matplotlib - Choosing Colormaps
- Matplotlib - Colorbars
- Matplotlib - Text
- Matplotlib - Text properties
- Matplotlib - Subplot Titles
- Matplotlib - Images
- Matplotlib - Image Masking
- Matplotlib - Annotations
- Matplotlib - Arrows
- Matplotlib - Fonts
- Matplotlib - What are Fonts?
- Setting Font Properties Globally
- Matplotlib - Font Indexing
- Matplotlib - Font Properties
- Matplotlib - Scales
- Matplotlib - Linear and Logarthmic Scales
- Matplotlib - Symmetrical Logarithmic and Logit Scales
- Matplotlib - LaTeX
- Matplotlib - What is LaTeX?
- Matplotlib - LaTeX for Mathematical Expressions
- Matplotlib - LaTeX Text Formatting in Annotations
- Matplotlib - PostScript
- Enabling LaTex Rendering in Annotations
- Matplotlib - Mathematical Expressions
- Matplotlib - Animations
- Matplotlib - Artists
- Matplotlib - Styling with Cycler
- Matplotlib - Paths
- Matplotlib - Path Effects
- Matplotlib - Transforms
- Matplotlib - Ticks and Tick Labels
- Matplotlib - Radian Ticks
- Matplotlib - Dateticks
- Matplotlib - Tick Formatters
- Matplotlib - Tick Locators
- Matplotlib - Basic Units
- Matplotlib - Autoscaling
- Matplotlib - Reverse Axes
- Matplotlib - Logarithmic Axes
- Matplotlib - Symlog
- Matplotlib - Unit Handling
- Matplotlib - Ellipse with Units
- Matplotlib - Spines
- Matplotlib - Axis Ranges
- Matplotlib - Axis Scales
- Matplotlib - Axis Ticks
- Matplotlib - Formatting Axes
- Matplotlib - Axes Class
- Matplotlib - Twin Axes
- Matplotlib - Figure Class
- Matplotlib - Multiplots
- Matplotlib - Grids
- Matplotlib - Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Subplots() Function
- Matplotlib - Subplot2grid() Function
- Matplotlib - Anchored Artists
- Matplotlib - Manual Contour
- Matplotlib - Coords Report
- Matplotlib - AGG filter
- Matplotlib - Ribbon Box
- Matplotlib - Fill Spiral
- Matplotlib - Findobj Demo
- Matplotlib - Hyperlinks
- Matplotlib - Image Thumbnail
- Matplotlib - Plotting with Keywords
- Matplotlib - Create Logo
- Matplotlib - Multipage PDF
- Matplotlib - Multiprocessing
- Matplotlib - Print Stdout
- Matplotlib - Compound Path
- Matplotlib - Sankey Class
- Matplotlib - MRI with EEG
- Matplotlib - Stylesheets
- Matplotlib - Background Colors
- Matplotlib - Basemap
- Matplotlib Event Handling
- Matplotlib - Event Handling
- Matplotlib - Close Event
- Matplotlib - Mouse Move
- Matplotlib - Click Events
- Matplotlib - Scroll Event
- Matplotlib - Keypress Event
- Matplotlib - Pick Event
- Matplotlib - Looking Glass
- Matplotlib - Path Editor
- Matplotlib - Poly Editor
- Matplotlib - Timers
- Matplotlib - Viewlims
- Matplotlib - Zoom Window
- Matplotlib Plotting
- Matplotlib - Bar Graphs
- Matplotlib - Histogram
- Matplotlib - Pie Chart
- Matplotlib - Scatter Plot
- Matplotlib - Box Plot
- Matplotlib - Violin Plot
- Matplotlib - Contour Plot
- Matplotlib - 3D Plotting
- Matplotlib - 3D Contours
- Matplotlib - 3D Wireframe Plot
- Matplotlib - 3D Surface Plot
- Matplotlib - Quiver Plot
- Matplotlib Useful Resources
- Matplotlib - Quick Guide
- Matplotlib - Useful Resources
- Matplotlib - Discussion
Matplotlib - Paths
A path is a route (track) that guides movement from one place to another. It can be physical, like a trail or road, or abstract, like a series of steps to achieve a goal. Paths provide direction and help us to navigate through spaces. They signify a way to reach a destination.
Paths in Matplotlib
In Matplotlib, paths are fundamental objects used to draw shapes and lines on plots. A path consists of a series of connected points, known as vertices or nodes, along with instructions on how to connect these points to form shapes like lines, curves, or polygons.
You can create a path in Matplotlib using the "Path" class. These paths can be used to create various types of plots such as lines, curves, polygons, and even custom shapes. Paths provide a way to define the appearance of objects in plots, allowing you to control their size, position, and style.
Let’s start by drawing a basic straight line path.
Straight Line Path
In Matplotlib, a straight line path refers to a basic geometric element consisting of two points connected by a straight line. This path is created by specifying the coordinates of the starting point (often called the "origin") and the ending point of the line. Matplotlib then draws a straight line between these two points, forming the path.
Example
In the following example, we are creating a straight line path using the Path class in Matplotlib. We define two vertices representing the starting and ending points of the line. Additionally, we specify the path codes to indicate that the path moves to the first vertex and then creates a line to the second vertex. By constructing the path using these vertices and codes, we achieve the representation of a straight line −
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches # Defining the vertices for the straight line path verts = [ # Starting point (0, 0), # Ending point (1, 1) ] # Defining the codes for the straight line path codes = [Path.MOVETO, Path.LINETO] # Creating the straight line path path = Path(verts, codes) # Plotting the path fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2) ax.add_patch(patch) ax.set_title('Straight Line Path') ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.grid(True) plt.show()
Output
Following is the output of the above code −
Circle Path
In Matplotlib, a circle path represents a circular shape drawn on a plot. It is created by specifying the center point of the circle and its radius. Matplotlib then generates a series of points around the circumference of the circle, connecting them to form a closed path.
Example
In here, we are creating a circular path using the Path class in Matplotlib. We generate a sequence of vertices along the circumference of a circle by defining angles uniformly spaced around the unit circle. Using trigonometric functions, we compute the coordinates of these vertices. Subsequently, we define the path codes to connect these vertices with lines −
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches import numpy as np # Defining the angles for the circle path theta = np.linspace(0, 2*np.pi, 100) # Defining the vertices for the circle path verts = np.column_stack([np.cos(theta), np.sin(theta)]) # Definng the codes for the circle path codes = [Path.MOVETO] + [Path.LINETO] * (len(verts) - 1) # Creating the circle path path = Path(verts, codes) # Plotting the path fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2) ax.add_patch(patch) ax.set_title('Circle Path') ax.set_xlim(-1.1, 1.1) ax.set_ylim(-1.1, 1.1) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.grid(True) plt.show()
Output
On executing the above code we will get the following output −
Bezier Curve Path
In Matplotlib, a Bezier curve path is a smooth, curved line created using control points to define its shape.
To construct a Bezier curve, you specify a series of control points that guide the curve's path. These control points determine the curve's direction and shape, allowing you to create smooth, flowing shapes. Matplotlib then generates the curve by connecting the dots these control points.
Example
Now, we are creating a Bezier curve path in Matplotlib using the Path class. We specify the control points that define the shape of the Bezier curve. Additionally, we define the path codes to indicate the type of curve segments between consecutive control points. By constructing the path with these control points and codes, we create a smooth Bezier curve −
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches # Defining the control points for the Bezier curve verts = [ (0.1, 0.1), (0.2, 0.8), (0.8, 0.8), (0.9, 0.1), ] # Defining the codes for the Bezier curve path codes = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4] # Creating the Bezier curve path path = Path(verts, codes) # Plotting the path fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2) ax.add_patch(patch) ax.set_title('Bezier Curve Path') ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.grid(True) plt.show()
Output
After executing the above code, we get the following output −
Polygon Path
In Matplotlib, a polygon path represents a closed shape with multiple straight sides, similar to a geometric polygon. This shape is created by specifying the coordinates of its vertices, or corner points, in a specific order. Matplotlib then connects these points sequentially to form the edges of the polygon and closes the path by connecting the last point to the first.
Polygon paths are versatile elements used to represent various shapes, such as triangles, rectangles, pentagons, or any other multi-sided figures. They can be filled with color to represent areas or used as outlines to highlight specific regions in plots.
Example
In this example, we create a polygonal path using the Path class in Matplotlib. We define the vertices of the polygon, representing the corners of the polygonal shape. Additionally, we specify the path codes to connect these vertices and close the polygon. By constructing the path with the vertices and codes, we form a closed polygonal shape −
import matplotlib.pyplot as plt from matplotlib.path import Path import matplotlib.patches as patches # Defining the vertices for the polygon verts = [ (0.1, 0.1), (0.5, 0.9), (0.9, 0.1), # Closing the polygon (0.1, 0.1) ] # Defining the codes for the polygon path codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] # Creating the polygon path path = Path(verts, codes) # Plotting the path fig, ax = plt.subplots() patch = patches.PathPatch(path, facecolor='none', lw=2) ax.add_patch(patch) ax.set_title('Polygon Path') ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.grid(True) plt.show()
Output
On executing the above code we will get the following output −