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.

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