Customizing Plot Axes

PythonPythonBeginner
Practice Now

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

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") 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 python/comments -.-> lab-48726{{"`Customizing Plot Axes`"}} matplotlib/importing_matplotlib -.-> lab-48726{{"`Customizing Plot Axes`"}} matplotlib/figures_axes -.-> lab-48726{{"`Customizing Plot Axes`"}} python/lists -.-> lab-48726{{"`Customizing Plot Axes`"}} python/tuples -.-> lab-48726{{"`Customizing Plot Axes`"}} python/importing_modules -.-> lab-48726{{"`Customizing Plot Axes`"}} python/data_visualization -.-> lab-48726{{"`Customizing Plot Axes`"}} end

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.

Other Python Tutorials you may like