Issue
How can I draw an area bounded by some linear inequality functions using matplotlib.
For example, if we have 3 functions:
y <= -2+4x
, y >= 2+0.5x
, y <= 7 -0.3x
I would like to draw something similar as wolfram alpha does: http://www3.wolframalpha.com/Calculate/MSP/MSP43251aca1dfd6ebcd862000067b9fd36a79h3igf?MSPStoreType=image/gif&s=39&w=200.&h=210.&cdf=Coordinates&cdf=Tooltips
Solution
I wrote a very simple example, valid for your problem only, but it's easily to extend and generalize it. The only trick is to use simpy to simplify the problem of finding the roots for to build the desired polygon. (Taken from http://docs.sympy.org/dev/modules/solvers/solvers.html)
import numpy as np
import matplotlib.pyplot as plt
from sympy.solvers import solve
from sympy import Symbol
def f1(x):
return 4.0*x-2.0
def f2(x):
return 0.5*x+2.0
def f3(x):
return -0.3*x+7.0
x = Symbol('x')
x1, = solve(f1(x)-f2(x))
x2, = solve(f1(x)-f3(x))
x3, = solve(f2(x)-f3(x))
y1 = f1(x1)
y2 = f1(x2)
y3 = f2(x3)
plt.plot(x1,f1(x1),'go',markersize=10)
plt.plot(x2,f1(x2),'go',markersize=10)
plt.plot(x3,f2(x3),'go',markersize=10)
plt.fill([x1,x2,x3,x1],[y1,y2,y3,y1],'red',alpha=0.5)
xr = np.linspace(0.5,7.5,100)
y1r = f1(xr)
y2r = f2(xr)
y3r = f3(xr)
plt.plot(xr,y1r,'k--')
plt.plot(xr,y2r,'k--')
plt.plot(xr,y3r,'k--')
plt.xlim(0.5,7)
plt.ylim(2,8)
plt.show()
Regards
Answered By - Pablo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.