- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
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
Draw a curve connecting two points instead of a straight line in matplotlib
To draw a curve connecting two points instead of a straight line in matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Define a draw_curve() method to make a curve with a mathematical expression.
- Plot point1 and point2 data points.
- Plot x and y data points returned from the draw_curve() method.
- To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True def draw_curve(p1, p2): a = (p2[1] - p1[1]) / (np.cosh(p2[0]) - np.cosh(p1[0])) b = p1[1] - a * np.cosh(p1[0]) x = np.linspace(p1[0], p2[0], 100) y = a * np.cosh(x) + b return x, y p1 = [0, 1] p2 = [1, 2] x, y = draw_curve(p1, p2) plt.plot(p1[0], p1[1], 'o') plt.plot(p2[0], p2[1], 'o') plt.plot(x, y) plt.show()
Output
It will produce the following output
- Related Articles
- Connecting two points on a 3D scatter plot in Python and Matplotlib
- Draw a parametrized curve using pyplot.plot() in Matplotlib
- Draw parametrized curve using pyplot.plot() in Matplotlib
- How to remove a specific line or curve in Matplotlib?
- How to draw a precision-recall curve with interpolation in Python Matplotlib?
- Program to find out the number of integral coordinates on a straight line between two points in Python
- How to draw a line outside of an axis in Matplotlib?
- Represent a Given Set of Points by the Best Possible Straight Line in C++
- If i am drawing a straight line without lifting my pencil then that is also a curve?
- How do you create line segments between two points in Matplotlib?
- Program to check whether list of points form a straight line or not in Python
- How to draw the largest polygon from a set of points in matplotlib?
- How can I draw a scatter trend line using Matplotlib?
- How to animate a sine curve in Matplotlib?
- How to draw an average line for a scatter plot in MatPlotLib?

Advertisements