Adding Lines to Figures

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to add lines to a figure without any axes using Matplotlib in Python.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48727{{"`Adding Lines to Figures`"}} matplotlib/figures_axes -.-> lab-48727{{"`Adding Lines to Figures`"}} python/lists -.-> lab-48727{{"`Adding Lines to Figures`"}} python/tuples -.-> lab-48727{{"`Adding Lines to Figures`"}} python/importing_modules -.-> lab-48727{{"`Adding Lines to Figures`"}} python/data_visualization -.-> lab-48727{{"`Adding Lines to Figures`"}} end

Import necessary libraries

First, we need to import the necessary libraries. We will be using matplotlib.pyplot and matplotlib.lines for this lab.

import matplotlib.pyplot as plt
import matplotlib.lines as lines

Create a Figure object

Next, we create a Figure object using the plt.figure() method.

fig = plt.figure()

Add lines to the Figure

We can add lines to the figure using the fig.add_artist() method. We will create two lines - one from (0,0) to (1,1) and another from (0,1) to (1,0).

fig.add_artist(lines.Line2D([0, 1], [0, 1]))
fig.add_artist(lines.Line2D([0, 1], [1, 0]))

Display the Figure

Finally, we display the figure using the plt.show() method.

plt.show()

Summary

In this lab, we learned how to add lines to a figure without any axes using Matplotlib in Python. We used matplotlib.pyplot and matplotlib.lines libraries to create a Figure object and add lines to it using fig.add_artist() method. We then displayed the figure using plt.show() method.

Other Matplotlib Tutorials you may like