'Graphing Inequalities in python
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()
Solution 1:[1]
You can combine multiple sympy plots via show=False and appending plots. This can be used to add lines:
from sympy import symbols, Eq, plot_implicit
x, y = symbols('x y')
p1 = plot_implicit(And(x > 3, y > x), show=False)
p2 = plot_implicit(Eq(x, 3), line_color='crimson', show=False)
p3 = plot_implicit(Eq(x, y), line_color='crimson', show=False)
p1.append(p2[0])
p1.append(p3[0])
p1.show()
Alternatively, you could make use of the markers= parameter to add lines or points. annotations= can be used to add text.
from sympy import symbols, Eq, plot_implicit
x, y = symbols('x y')
plot_implicit(And(x > 3, y > x),
markers=[{'args': [[3, 3], [-5, 5]], 'color': 'r', 'ls': '--'},
{'args': [[-5, 5], [-5, 5]], 'color': 'r', 'ls': '--'}],
annotations=[{'xy': (3, 2), 'text': " $x = 3$",
'ha': 'left', 'va': 'center', 'color': 'r'},
{'xy': (2, 2), 'text': "$x = y$",
'ha': 'right', 'va': 'center', 'color': 'r', 'rotation': 45}])
Solution 2:[2]
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:
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.
Solution 3:[3]
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 SymPy Plotting Backend module which is an overall improvement to what SymPy is currently offering (disclamair: I am the developer of this module. Hopefully one day it will be part of SymPy).
Here is an example:
from sympy import var
from spb import * # SymPy Plotting Module
var("x, y")
c = 3
# a boolean expression composed of multiple inequalities
expr = (y < x + c) & (y < -x + c) & (y > x - c) & (y > -x - c)
# assuming y is on the LHS of the inequality, here we extract
# the RHS, which are going to create the limiting lines
expressions = []
for a in expr.args:
rhs = a.args[1]
# append to expression the tuple (RHS, label)
expressions.append((rhs, str(a)))
# plot the limiting lines
p1 = plot(*expressions, (x, -5, 5), aspect="equal",
line_kw={"linestyle": "--"})
# plot the region represented by the overall expression
p2 = plot_implicit(expr, (x, -5, 5), (y, -5, 5))
# combine the plots
(p1 + p2).show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Davide_sd |




