0

I have graphed linear inequalities with matplotlib. How can I print the equations along the lines ? I do not want to label them on the right but right on the top of every lines.

Here is my code for the moment :

import numpy as np
import matplotlib.pyplot as plt


# Construct lines
# x > 0
x = np.linspace(0, 20, 2000)
# x1 >= 0
y1 = (x*0) + 0
# 2x1+x2<=10
y2 = 10-2*x
# x2 >= 4x-8
y3 = (4*x-8)/4.0
# y <= 2x - 5 
y4 = (5 * x +2)/2
plt.xlabel(r'$x_2>=0$')
plt.ylabel(r'$x_1>=0$')
# Make plot
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.plot(x, y4)
plt.xlim((0, 16))
plt.ylim((0, 11))


# Fill feasible region
y5 = np.minimum(y2, y4)
y6 = np.maximum(y1, y3)
plt.fill_between(x, y5, y6, where=y5>y6, color='grey', alpha=0.5)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.grid()

plt.show()
Twister
  • 1
  • 1
  • Hi and welcome to SO, please provide a Minimal, Reproducible Example (https://stackoverflow.com/help/reprex) – ilja May 16 '19 at 08:38
  • Sorry. There it is – Twister May 16 '19 at 09:10
  • You can use `text()` and plot it on a specific position, e.g. `plt.text(8, 0.5, r'$x_1>=0$')`. I am not sure if there is a better solution, but maybe this helps: https://stackoverflow.com/questions/16992038/inline-labels-in-matplotlib – ilja May 16 '19 at 09:49

0 Answers0