
- 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 a sine function 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 a sine function in a plot −
EXample
import matplotlib.pyplot as plt import numpy as np data = np.arange(0.0, 4.0, 0.1) y = 2 + np.sin(2 * np.pi * data) fig, ax = plt.subplots() ax.plot(data, y) ax.set(xlabel='x-axis data', ylabel='y-axis data',title='A simple plot') ax.grid() plt.show()
Output
Explanation
The required packages are imported and its alias is defined for ease of use.
The data is created using NumPy package.
An empty figure is created using the ‘figure’ function.
The ‘subplot’ function is used to create outlines for three different plots.
The data is plotted using the ‘plot’ function.
The set function is used to provide labels for ‘X’ and ‘Y’ axis.
The title of the plot is defined.
It is shown on the console using the ‘show’ function.
- Related Articles
- How can the ‘subplot’ function be used to create two graphs in Matplotlib Python?
- How can matplotlib be used to create histograms using Python?
- Explain how Matplotlib can be used to create a wireframe plot in Python?
- How can Matplotlib be used to create multiple plots iteratively in 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?
- Can generator be used to create iterators in Python?
- How can Tensorflow be used to create a feature extractor using Python?
- How can Tensorflow be used to create a convolutional base using Python?
- How can Tensorflow be used to create a sequential model using Python?
- How can Matplotlib be used to three-dimensional line plots using Python?
- How can Bokeh be used to create step line plot in Python?
- How can matplotlib be used to plot 3 different datasets on a single graph in Python?
- How to animate a sine curve in Matplotlib?
- How can Tensorflow be used to create an input function to to train the model?
