- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 do I find the intersection of two line segments in Matplotlib?
63 Lectures 6 hours
To find the intersection of two lines segments in Matplotlib and pass the horizontal and vertical lines through that point, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create two lines using slopes (m1, m2) and intercepts (c1 and c2). Initialize the slopes and intercept values.
- Create x data points using numpy.
- Plot x, m1, m2, c2 and c1 data points using plot() method.
- Using intercepts and slope values, find the point of intersection.
- Plot the horizontal and vertical lines with dotted linestyle.
- Plot xi and yi points on the plot.
- To display the figure, use show() method.
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True m1, c1 = 0.1, 2.0 m2, c2 = 2.0, -3.0 x = np.linspace(-10, 10, 500) plt.plot(x, x * m1 + c1, 'red') plt.plot(x, x * m2 + c2, 'green') plt.xlim(-2, 8) plt.ylim(-2, 8) xi = (c1 - c2) / (m2 - m1) yi = m1 * xi + c1 plt.axvline(x=xi, color='gray', linestyle='--') plt.axhline(y=yi, color='gray', linestyle='--') plt.scatter(xi, yi, color='black') plt.show()
- How do you create line segments between two points in Matplotlib?
- Maximum possible intersection by moving centers of line segments in C++
- Check if two line segments intersect
- How to find the intersection of two arrays in java?
- Find the Intersection Point of Two Linked Lists in Java
- C# program to find Intersection of two lists
- Python program to find Intersection of two lists?
- How to find intersection between two Numpy arrays?
- Coloring the Intersection of Circles/Patches in Matplotlib
- C++ program to find the number of triangles amongst horizontal and vertical line segments
- How do I configure the behavior of the Qt4Agg backend in Matplotlib?
- Program to find number of sets of k-non-overlapping line segments in Python
- How can I cycle through line styles in Matplotlib?
- How can I draw inline line labels in Matplotlib?