Creating a Line Plot

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to create a line plot using Python Matplotlib. A line plot is a way of displaying data points on a line. It is used to show the trend of a particular set of data over time.

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/FileHandlingGroup(["`File Handling`"]) 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/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") 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/titles_labels("`Adding Titles and Labels`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/with_statement -.-> lab-48869{{"`Creating a Line Plot`"}} matplotlib/importing_matplotlib -.-> lab-48869{{"`Creating a Line Plot`"}} matplotlib/figures_axes -.-> lab-48869{{"`Creating a Line Plot`"}} matplotlib/line_plots -.-> lab-48869{{"`Creating a Line Plot`"}} matplotlib/titles_labels -.-> lab-48869{{"`Creating a Line Plot`"}} python/tuples -.-> lab-48869{{"`Creating a Line Plot`"}} python/importing_modules -.-> lab-48869{{"`Creating a Line Plot`"}} python/standard_libraries -.-> lab-48869{{"`Creating a Line Plot`"}} python/math_random -.-> lab-48869{{"`Creating a Line Plot`"}} python/numerical_computing -.-> lab-48869{{"`Creating a Line Plot`"}} python/data_visualization -.-> lab-48869{{"`Creating a Line Plot`"}} end

Importing Libraries

First, we need to import the necessary libraries. We will be using the matplotlib.pyplot library to create our line plot.

import matplotlib.pyplot as plt
import numpy as np

Creating Data

Next, we need to create some data that we will plot on our line. We will use NumPy to create some random data points for our line.

x = np.linspace(0, 10)

Creating the Line Plot

Now we can create our line plot using the plot() function from matplotlib.pyplot. We will plot 8 random lines using different colors from the Solarized Light color scheme.

with plt.style.context('Solarize_Light2'):
    plt.plot(x, np.sin(x) + x + np.random.randn(50))
    plt.plot(x, np.sin(x) + 2 * x + np.random.randn(50))
    plt.plot(x, np.sin(x) + 3 * x + np.random.randn(50))
    plt.plot(x, np.sin(x) + 4 + np.random.randn(50))
    plt.plot(x, np.sin(x) + 5 * x + np.random.randn(50))
    plt.plot(x, np.sin(x) + 6 * x + np.random.randn(50))
    plt.plot(x, np.sin(x) + 7 * x + np.random.randn(50))
    plt.plot(x, np.sin(x) + 8 * x + np.random.randn(50))

Adding Labels and Title

We can add labels and a title to our line plot using the xlabel(), ylabel(), and title() functions.

plt.title('8 Random Lines - Line')
plt.xlabel('x label', fontsize=14)
plt.ylabel('y label', fontsize=14)

Displaying the Plot

Finally, we can display our line plot using the show() function.

plt.show()

Summary

In this lab, we learned how to create a line plot using Python Matplotlib. We imported the necessary libraries, created some data, and used the plot() function to create our line plot. We added labels and a title to our plot and displayed it using the show() function.

Other Python Tutorials you may like