

- 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 to plot a function defined with def in Python? (Matplotlib)
To plot a function defined with def in Python, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a user-defined function using, def, i.e., f(x).
- Create x data points using numpy.
- Plot x and f(x) using plot() method.
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True def f(x): return np.sin(x) + x + x * np.sin(x) x = np.linspace(-10, 10, 100) plt.plot(x, f(x), color='red') plt.show()
Output
- Related Questions & Answers
- How to plot a multivariate function in Python Matplotlib?
- How do I plot a step function with Matplotlib in Python?
- How to a plot stem plot in Matplotlib Python?
- How to save a plot in Seaborn with Python (Matplotlib)?
- How to plot a 3D density map in Python with Matplotlib?
- How to plot a 2D matrix in Python with colorbar Matplotlib?
- How to create a Swarm Plot with Matplotlib?
- How to plot a Pandas Dataframe with Matplotlib?
- How to plot a smooth line with matplotlib?
- How to plot pie-chart with a single pie highlighted with Python Matplotlib?
- How to plot a density map in Python Matplotlib?
- How to plot a histogram using Matplotlib in Python with a list of data?
- How to plot with xgboost.XGBCClassifier.feature_importances_ model? (Matplotlib)
- How to plot a layered image in Matplotlib in Python?
- How to plot a phase spectrum in Matplotlib in Python?
Advertisements