- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 −

## Example

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()

## Output

## Explanation

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 Questions & Answers
- 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 Bokeh be used to visualize multiple shapes on a plot in Python?
- How can Matplotlib be used to create three-dimensional scatter plot using Python?
- How can I plot a single point in Matplotlib Python?
- How can I plot two different spaced time series on one same plot in Python Matplotlib?
- How can matplotlib be used to create a sine function in Python?
- How can Bokeh be used to generate patch plot in Python?
- How can Bokeh be used to visualize bar plot in Python?
- How can matplotlib be used to create histograms using 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 Tensorflow be used to download a single image to try the model on using Python?
- How can Tensorflow be used to plot the results using Python?
- How can Keras be used to plot the model using Python?