
- 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
How can Matplotlib be used to create multiple plots iteratively 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 multiple plots in the same plot iteratively −
Example
import numpy as np import matplotlib.pyplot as plt fig = plt.figure() fig.subplots_adjust(top=0.8) ax1 = fig.add_subplot(211) ax1.set_ylabel('Y−axis') ax1.set_title('A simple plot') t = np.arange(0.0, 1.0, 0.01) s = np.sin(2*np.pi*t) line, = ax1.plot(t, s, color='blue', lw=2) np.random.seed(4567232) ax2 = fig.add_axes([0.15, 0.1, 0.8, 0.3]) n, bins, patches = ax2.hist(np.random.randn(1000), 50, facecolor='yellow', edgecolor='green') ax2.set_xlabel('x−label') plt.show()
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 ‘subplot’ function is used to create an area to plot the graph.
The data values are created using the NumPy library.
The ‘random’ library’s ‘seed’ function is used to create data points.
The ‘add_subplot’ is used to create another new subplot to the newly created plot.
The data is plotted using the ‘plot’ function.
The set_xlabel, set_ylabel and set_title functions are used to provide labels for ‘X’ axis, ‘Y’ axis and title.
It is shown on the console using the ‘show’ function.
- Related Articles
- How can Bokeh be used to visualize multiple bar plots in Python?
- How can Matplotlib be used to three-dimensional line plots using Python?
- How can multiple plots be plotted in same figure using matplotlib and Python?
- How can matplotlib be used to create histograms using Python?
- How can matplotlib be used to create a sine function in Python?
- How can Pygal be used to generate line plots in Python?
- How can Pygal be used to generate box plots in Python?
- How can Pygal be used to generate dot plots in Python?
- How can Pygal be used to generate Funnel plots in Python?
- How can Pygal be used to generate Gauge plots in Python?
- Explain how Matplotlib can be used to create a wireframe plot in Python?
- How can Seaborn library be used to visualize point plots in Python?
- How can the ‘subplot’ function be used to create two graphs in Matplotlib Python?
- How can Matplotlib be used to create three-dimensional scatter plot using Python?
- How can Matplotlib be used to create 3 dimensional contour plot using Python?
