Plotting With Keywords

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to generate plots using strings corresponding to variables using the numpy.recarray or pandas.DataFrame data format with the data keyword argument in 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 matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48795{{"`Plotting With Keywords`"}} matplotlib/figures_axes -.-> lab-48795{{"`Plotting With Keywords`"}} matplotlib/scatter_plots -.-> lab-48795{{"`Plotting With Keywords`"}} python/variables_data_types -.-> lab-48795{{"`Plotting With Keywords`"}} python/lists -.-> lab-48795{{"`Plotting With Keywords`"}} python/tuples -.-> lab-48795{{"`Plotting With Keywords`"}} python/dictionaries -.-> lab-48795{{"`Plotting With Keywords`"}} python/importing_modules -.-> lab-48795{{"`Plotting With Keywords`"}} python/standard_libraries -.-> lab-48795{{"`Plotting With Keywords`"}} python/math_random -.-> lab-48795{{"`Plotting With Keywords`"}} python/data_collections -.-> lab-48795{{"`Plotting With Keywords`"}} python/numerical_computing -.-> lab-48795{{"`Plotting With Keywords`"}} python/data_visualization -.-> lab-48795{{"`Plotting With Keywords`"}} python/build_in_functions -.-> lab-48795{{"`Plotting With Keywords`"}} end

Import Required Libraries

In this step, we will import the necessary libraries to generate plots using keywords.

import matplotlib.pyplot as plt
import numpy as np

Set Seed Value

In this step, we will set the seed value for the random number generator to ensure that the results are reproducible.

np.random.seed(19680801)

Create Data

In this step, we will create a dictionary data containing values for variables a, b, c, and d.

data = {'a': np.arange(50),
        'c': np.random.randint(0, 50, 50),
        'd': np.random.randn(50)}

data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100

Generate Plot

In this step, we will generate a scatter plot using the data dictionary as input to the scatter() function. We will use the strings corresponding to the a, b, c, and d variables to generate the plot.

fig, ax = plt.subplots()
ax.scatter('a', 'b', c='c', s='d', data=data)
ax.set(xlabel='entry a', ylabel='entry b')
plt.show()

Interpret the Plot

In this step, we will interpret the scatter plot generated in Step 4. The plot shows the relationship between the a and b variables, with the c variable used to determine the color of each point and the d variable used to determine the size of each point. The x-axis represents entry a and the y-axis represents entry b.

Summary

In this lab, you learned how to generate plots using strings corresponding to variables using the numpy.recarray or pandas.DataFrame data format with the data keyword argument in Matplotlib. You also learned how to interpret a scatter plot using the a, b, c, and d variables.

Other Python Tutorials you may like