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.
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.