Matplotlib Line Plot With Data Points

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 with data points using Matplotlib in Python. We will use the EventCollection class in Matplotlib to mark the locations of the x and y data points on the respective axes for each curve.

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`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) 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/BasicConceptsGroup -.-> python/comments("`Comments`") 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/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") 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`") subgraph Lab Skills python/comments -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/with_statement -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} matplotlib/importing_matplotlib -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} matplotlib/figures_axes -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} matplotlib/line_plots -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} matplotlib/event_handling -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/variables_data_types -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/lists -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/tuples -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/importing_modules -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/using_packages -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/standard_libraries -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/math_random -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/data_collections -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/numerical_computing -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} python/data_visualization -.-> lab-48720{{"`Matplotlib Line Plot With Data Points`"}} end

Import necessary libraries

First, we need to import the required libraries. We will use numpy to create random data, matplotlib.pyplot to create the plot, and EventCollection from matplotlib.collections to mark the locations of data points.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import EventCollection

Create random data

We will create random data for two curves using numpy.random.random() function. We will generate two sets of 10 random numbers between 0 and 1 and store them in an array.

## create random data
xdata = np.random.random([2, 10])

Sort the data

To make clean curves, we will sort the data using the sort() method.

## split the data into two parts
xdata1 = xdata[0, :]
xdata2 = xdata[1, :]
## sort the data so it makes clean curves
xdata1.sort()
xdata2.sort()

Create y data points

We will create some y data points for each curve by performing some mathematical operations on the sorted x data points.

## create some y data points
ydata1 = xdata1 ** 2
ydata2 = 1 - xdata2 ** 3

Create the plot

We will create the plot using matplotlib.pyplot.plot() function.

## plot the data
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(xdata1, ydata1, color='tab:blue')
ax.plot(xdata2, ydata2, color='tab:orange')

Create the events

We will create the events marking the x and y data points using EventCollection() function.

## create the events marking the x data points
xevents1 = EventCollection(xdata1, color='tab:blue', linelength=0.05)
xevents2 = EventCollection(xdata2, color='tab:orange', linelength=0.05)

## create the events marking the y data points
yevents1 = EventCollection(ydata1, color='tab:blue', linelength=0.05,
                           orientation='vertical')
yevents2 = EventCollection(ydata2, color='tab:orange', linelength=0.05,
                           orientation='vertical')

Add the events to the plot

We will add the events to the plot using matplotlib.pyplot.add_collection() function.

## add the events to the axis
ax.add_collection(xevents1)
ax.add_collection(xevents2)
ax.add_collection(yevents1)
ax.add_collection(yevents2)

Set the limits and add title

We will set the limits of the x and y axes and add a title to the plot using matplotlib.pyplot.xlim(), matplotlib.pyplot.ylim(), and matplotlib.pyplot.title() functions.

## set the limits
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_title('line plot with data points')

Display the plot

Finally, we will display the plot using matplotlib.pyplot.show() function.

## display the plot
plt.show()

Summary

In this lab, we learned how to create a line plot with data points using Matplotlib in Python. We used the EventCollection class in Matplotlib to mark the locations of the x and y data points on the respective axes for each curve.

Other Python Tutorials you may like