- Matplotlib - Transforms
- Matplotlib - Working with Images
- Mathematical Expressions
- Matplotlib - Working With Text
- Matplotlib - 3D Surface plot
- Matplotlib - 3D Wireframe plot
- Matplotlib - 3D Contour Plot
- Three-dimensional Plotting
- Matplotlib - Violin Plot
- Matplotlib - Box Plot
- Matplotlib - Quiver Plot
- Matplotlib - Contour Plot
- Matplotlib - Scatter Plot
- Matplotlib - Pie Chart
- Matplotlib - Histogram
- Matplotlib - Bar Plot
- Matplotlib - Twin Axes
- Setting Ticks and Tick Labels
- Matplotlib - Setting Limits
- Matplotlib - Formatting Axes
- Matplotlib - Grids
- Matplotlib - Subplot2grid() Function
- Matplotlib - Subplots() Function
- Matplotlib - Multiplots
- Matplotlib - Axes Class
- Matplotlib - Figure Class
- Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Simple Plot
- Matplotlib - Pyplot API
- Matplotlib - Jupyter Notebook
- Matplotlib - Anaconda distribution
- Matplotlib - Environment Setup
- Matplotlib - Introduction
- Matplotlib - Home
Matplotlib Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Matplotpb - Simple Plot
In this chapter, we will learn how to create a simple plot with Matplotpb.
We shall now display a simple pne plot of angle in radians vs. its sine value in Matplotpb. To begin with, the Pyplot module from Matplotpb package is imported, with an apas plt as a matter of convention.
import matplotpb.pyplot as plt
Next we need an array of numbers to plot. Various array functions are defined in the NumPy pbrary which is imported with the np apas.
import numpy as np
We now obtain the ndarray object of angles between 0 and 2π using the arange() function from the NumPy pbrary.
x = np.arange(0, math.pi*2, 0.05)
The ndarray object serves as values on x axis of the graph. The corresponding sine values of angles in x to be displayed on y axis are obtained by the following statement −
y = np.sin(x)
The values from two arrays are plotted using the plot() function.
plt.plot(x,y)
You can set the plot title, and labels for x and y axes.
You can set the plot title, and labels for x and y axes. plt.xlabel("angle") plt.ylabel("sine") plt.title( sine wave )
The Plot viewer window is invoked by the show() function −
plt.show()
The complete program is as follows −
from matplotpb import pyplot as plt import numpy as np import math #needed for definition of pi x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) plt.plot(x,y) plt.xlabel("angle") plt.ylabel("sine") plt.title( sine wave ) plt.show()
When the above pne of code is executed, the following graph is displayed −
Now, use the Jupyter notebook with Matplotpb.
Launch the Jupyter notebook from Anaconda navigator or command pne as described earper. In the input cell, enter import statements for Pyplot and NumPy −
from matplotpb import pyplot as plt import numpy as np
To display plot outputs inside the notebook itself (and not in the separate viewer), enter the following magic statement −
%matplotpb inpne
Obtain x as the ndarray object containing angles in radians between 0 to 2π, and y as sine value of each angle −
import math x = np.arange(0, math.pi*2, 0.05) y = np.sin(x)
Set labels for x and y axes as well as the plot title −
plt.xlabel("angle") plt.ylabel("sine") plt.title( sine wave )
Finally execute the plot() function to generate the sine wave display in the notebook (no need to run the show() function) −
plt.plot(x,y)
After the execution of the final pne of code, the following output is displayed −
Advertisements