- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How can matplotlib be used to plot 3 different datasets on a single graph in Python?
Matplotlib is a popular Python package that is used for data visualization. Visualizing data is a key step since it helps understand what is going on in the data without actually looking at the numbers and performing complicated computations. It helps in communicating the quantitative insights to the audience effectively.
Matplotlib is used to create 2 dimensional plots with the data. It comes with an object−oriented API that helps in embedding the plots in Python applications. Matplotlib can be used with IPython shells, Jupyter notebook, Spyder IDE and so on. It is written in Python. It is created using Numpy, which is the Numerical Python package in Python.
Python can be installed on Windows using the below command −
pip install matplotlib
The dependencies of Matplotlib are −
Python ( greater than or equal to version 3.4) NumPy Setuptools Pyparsing Libpng Pytz Free type Six Cycler Dateutil
Let us understand how Matplotlib can be used to plot 3 different data sets in a single plot −
import matplotlib.pyplot as plt import numpy as np fig = plt.figure() x = np.linspace(0, 2, 100) fig, ax = plt.subplots() ax.plot(x, x, label='linear') ax.plot(x, x**2, label='quadratic') ax.plot(x, x**3, label='cubic') ax.set_xlabel('x label name') ax.set_ylabel('y label name') ax.set_title("My Plot") ax.legend()
The required packages are imported and its alias is defined for ease of use.
An empty figure is created using the ‘figure’ function.
The data is created using NumPy package.
The ‘subplot’ function is used to create outlines for three different plots.
The type of plot for each of the three datasets is defined.
The labels for ‘X’ and ‘Y’ axis is defined.
The title of the plot is defined, and it is shown on the console.
- Related Articles
- How can Matplotlib be used to create 3 dimensional contour plot using Python?
- Explain how Matplotlib can be used to create a wireframe plot in Python?
- How can I plot a single point in Matplotlib Python?
- How can Matplotlib be used to create three-dimensional scatter plot using Python?
- How can Bokeh be used to visualize multiple shapes on a plot in Python?
- How can I plot two different spaced time series on one same plot in Python Matplotlib?
- How to Add Markers to a Graph Plot in Matplotlib with Python?
- How can matplotlib be used to create a sine function in Python?
- How to plot y=1/x as a single graph in Python?
- How can Bokeh be used to generate patch plot in Python?
- How can Bokeh be used to visualize bar plot in Python?
- Explain how a quiver plot can be built using Matplotlib Python?
- How can Seaborn library be used to display a Scatter Plot in Python?
- How can Seaborn library be used to display a hexbin plot in Python?
- How can box and whisker plot be used to compare the data in different categories in Python Seaborn?