Customizing Matplotlib Defaults with Python

PythonPythonBeginner
Practice Now

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

Introduction

This lab will guide you through the process of customizing .rcParams on the fly using Python Matplotlib. You will learn how to define functions in a custom module that set the defaults for figures, and how to use these defaults to create different sets of defaults for figures, such as one set for publication and another set for interactive exploration.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedPlottingGroup(["`Advanced Plotting`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) 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`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/AdvancedPlottingGroup -.-> matplotlib/subplots("`Subplots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/line_styles_colors("`Customizing Line Styles and Colors`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} matplotlib/importing_matplotlib -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} matplotlib/figures_axes -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} matplotlib/line_plots -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} matplotlib/subplots -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} matplotlib/line_styles_colors -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} matplotlib/grid_config -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} matplotlib/matplotlib_config -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} python/booleans -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} python/lists -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} python/tuples -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} python/dictionaries -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} python/function_definition -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} python/importing_modules -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} python/data_visualization -.-> lab-48646{{"`Customizing Matplotlib Defaults with Python`"}} end

Create a Function to Set Default Parameters

To create a function that sets the default parameters for your figures, you can use the rcParams.update() method. This method takes a dictionary of parameter names and values, and updates the current default values with the new ones. Here's an example of a function that sets some default parameters for publication figures:

def set_pub():
    rcParams.update({
        "font.weight": "bold",  ## bold fonts
        "tick.labelsize": 15,   ## large tick labels
        "lines.linewidth": 1,   ## thick lines
        "lines.color": "k",     ## black lines
        "grid.color": "0.5",    ## gray gridlines
        "grid.linestyle": "-",  ## solid gridlines
        "grid.linewidth": 0.5,  ## thin gridlines
        "savefig.dpi": 300,     ## higher resolution output.
    })

Customize Default Parameters

To customize the default parameters for a specific figure, you can use the rcParams.update() method again. This time, you'll pass a dictionary of parameter names and values that you want to set for that figure. Here's an example that sets some default parameters for a specific figure:

import matplotlib.pyplot as plt

plt.rcParams.update({
    "font.weight": "bold",
    "xtick.major.size": 5,
    "xtick.major.pad": 7,
    "xtick.labelsize": 15,
    "grid.color": "0.5",
    "grid.linestyle": "-",
    "grid.linewidth": 5,
    "lines.linewidth": 2,
    "lines.color": "g",
})

Create Subplots

To create subplots in Matplotlib, you can use the subplot() method. This method takes three arguments: the number of rows, the number of columns, and the plot number. Here's an example that creates three subplots:

plt.subplot(311)
plt.plot([1, 2, 3])

plt.subplot(312)
plt.plot([1, 2, 3])
plt.grid(True)

plt.subplot(313)
plt.plot([1, 2, 3])
plt.grid(True)

Show the Figure

To show the figure, you can use the show() method. Here's an example:

plt.show()

Reset Default Parameters

To reset the default parameters to their original values, you can use the rcdefaults() method. Here's an example:

plt.rcdefaults()

Summary

In this lab, you learned how to customize .rcParams on the fly using Python Matplotlib. You learned how to define functions that set the default parameters for your figures, how to customize the default parameters for a specific figure, how to create subplots, how to show the figure, and how to reset the default parameters to their original values. With these skills, you can create customized figures for your publications and interactive explorations.

Other Python Tutorials you may like