Home > Back-end >  Graphing Inequalities in python
Graphing Inequalities in python

Time:05-03

I'm looking to create a program that will randomly generate lines (that are inequalities) and that will show the area that satisfies the constraints.

I don't mind which libraries are used so feel free to use sympy, numpy etc

I will show my current code but this just fills the area between 2 lines and doesn't use inequalities at all.

If possible a legend would be nice but I can always add one myself.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,100,0.1)
y1 = 2*x
y2 = 0.5*x 5
plt.ylim(0, 20)
plt.xlim(0, 20)

# Plotting of lines
plt.plot(x, y1,
         x, y2)

# Filling between line y3 and line y4
plt.fill_between(x, y1, y2, color='grey', alpha=0.5)
plt.show()

CodePudding user response:

Actually using sympy you can do it in just one line. (As op mentioned inequalities and all)

from sympy import *
x, y, z, t = symbols('x y z t')
p1 = plot_implicit(And(x y > 1, y < 10, x**2 > y))

Plot:

enter image description here

Note: OP is talking about the area that satisfies the constraint. For 3 lines having only equality will give rise to collinear line or 3, 2 or 1 points. It won't give us an area unless we use inequality. Also sympy allows equalities as well in plot_implicit.

CodePudding user response:

Sympy does provide plot_implicit, which is a good starting point in the sense that it shows the filled area represented by the overall inequality. However, it is hardly good enough to show the limiting curves, which the OP has explicitely asked about. Since the OP doesn't mind the libraries being used, I'm going to use the enter image description here

  • Related