Introduction
In this tutorial, we will learn how to customize the background, labels, and ticks of a simple plot using Matplotlib.
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.
Importing necessary libraries
We will start by importing the necessary libraries that we will use in this tutorial.
import matplotlib.pyplot as plt
Creating a figure and setting the background
We will create a figure using the plt.figure() method, which creates a matplotlib.figure.Figure instance. We will set the background color of the figure using the rect.set_facecolor() method.
fig = plt.figure()
rect = fig.patch ## a rectangle instance
rect.set_facecolor('lightgoldenrodyellow')
Adding axes to the figure
We will add axes to the figure using the fig.add_axes() method. We will also set the background color of the axes using the rect.set_facecolor() method.
ax1 = fig.add_axes([0.1, 0.3, 0.4, 0.4])
rect = ax1.patch
rect.set_facecolor('lightslategray')
Customizing the ticks and labels
We will customize the ticks and labels of the axes using the ax1.tick_params() method. We will set the color, rotation, and size of the x-axis label, and the color, size, and width of the y-axis ticks.
ax1.tick_params(axis='x', labelcolor='tab:red', labelrotation=45, labelsize=16)
ax1.tick_params(axis='y', color='tab:green', size=25, width=3)
Displaying the plot
Finally, we will display the plot using the plt.show() method.
plt.show()
Summary
In this tutorial, we learned how to customize the background, labels, and ticks of a simple plot using Matplotlib. We used the plt.figure(), fig.add_axes(), ax1.tick_params(), and plt.show() methods to create and display the plot.