- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 smooth line with matplotlib?
To plot a smooth line with matplotlib, we can take the following steps −
Set the figure size and adjust the padding between and around the subplots.
Create a list of data points, x and y.
Plot the x and y data points.
Create x_new and bspline data points for smooth line.
Get y_new data points. Compute the (coefficients of) interpolating B-spline.
Plot x_new and y_new data points using plot() method.
To display the figure, use show() method.
import numpy as np from matplotlib import pyplot as plt from scipy import interpolate # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # x and y data points x = np.array([1, 3, 4, 6, 7]) y = np.array([5, 1, 3, 2, 4]) # Plot the data points plt.plot(x, y) # x_new, bspline, y_new x_new = np.linspace(1, 5, 50) bspline = interpolate.make_interp_spline(x, y) y_new = bspline(x_new) # Plot the new data points plt.plot(x_new, y_new) plt.show()
It will produce the following output −
- Related Articles
- How to plot a smooth 2D color plot for z = f(x, y) in Matplotlib?
- How to plot a line (polygonal chain) with matplotlib with minimal smoothing?
- How to animate a line plot in Matplotlib?
- Line plot with arrows in Matplotlib
- How to plot a 3D continuous line in Matplotlib?
- How to plot a gradient color line in matplotlib?
- Plot a Line Graph for Pandas Dataframe with Matplotlib?
- How to plot a line in Matplotlib with an interval at each data point?
- How to plot a line graph from histogram data in Matplotlib?
- How to plot a multi-colored line, like a rainbow using Matplotlib?
- How to plot a dashed line on a Seaborn lineplot in Matplotlib?
- How to create a Swarm Plot with Matplotlib?
- How to plot a Pandas Dataframe with Matplotlib?
- How to draw an average line for a scatter plot in MatPlotLib?
- How to plot a single line in Matplotlib that continuously changes color?