Set and Get

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a Python library used for creating static, animated, and interactive visualizations in Python. It allows you to create a wide range of visualizations such as line charts, scatter plots, bar charts, histograms, and 3D plots. In this tutorial, we will learn how to use the pyplot interface to set and get object properties for visualizing data.

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`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) 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`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/line_styles_colors("`Customizing Line Styles and Colors`") 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-48923{{"`Set and Get`"}} matplotlib/figures_axes -.-> lab-48923{{"`Set and Get`"}} matplotlib/line_plots -.-> lab-48923{{"`Set and Get`"}} matplotlib/line_styles_colors -.-> lab-48923{{"`Set and Get`"}} python/lists -.-> lab-48923{{"`Set and Get`"}} python/tuples -.-> lab-48923{{"`Set and Get`"}} python/importing_modules -.-> lab-48923{{"`Set and Get`"}} python/data_visualization -.-> lab-48923{{"`Set and Get`"}} end

Installing Matplotlib

Before we begin, we need to install Matplotlib using the following command in the terminal or command prompt.

!pip install matplotlib

Importing Matplotlib

To use Matplotlib, we need to import it in our Python script using the following import statement.

import matplotlib.pyplot as plt

Setting Properties

The pyplot interface allows us to set and get object properties for visualizing data. We can use the setp method to set the properties of an object. For example, to set the linestyle of a line to dashed, we use the following code:

line, = plt.plot([1, 2, 3])
plt.setp(line, linestyle='--')

If we want to know the valid types of arguments, we can provide the name of the property we want to set without a value:

plt.setp(line, 'linestyle')

This will return the following output:

linestyle: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}

If we want to see all the properties that can be set, and their possible values, we can use the following code:

plt.setp(line)

This will return a long list of properties and their possible values.

Getting Properties

We can use the getp method to get the properties of an object. We can use it to query the value of a single attribute:

plt.getp(line, 'linewidth')

This will return the value of the linewidth property of the line object.

We can also use getp to get all the attribute/value pairs of an object:

plt.getp(line)

This will return a long list of all the properties and their values.

Aliases

To reduce keystrokes in interactive mode, a number of properties have short aliases, e.g., 'lw' for 'linewidth' and 'mec' for 'markeredgecolor'. When calling set or get in introspection mode, these properties will be listed as 'fullname' or 'aliasname'.

l1, l2 = plt.plot([1, 2, 3], [2, 3, 4], [1, 2, 3], [3, 4, 5])
plt.setp(l1, linewidth=2, color='r')
plt.setp(l2, linewidth=1, color='g')

Summary

In this tutorial, we learned how to use the pyplot interface in Matplotlib to set and get object properties for visualizing data. We used the setp method to set the properties of an object and the getp method to get the properties of an object. We also learned about aliases for properties to reduce keystrokes. Matplotlib is a powerful library that allows you to create a wide range of visualizations in Python.

Other Python Tutorials you may like