
# graphs.py
# https://matplotlib.org/
'''
  This script can be executed directyly from Python
  or via JEP as shown in JepPlot.java
'''

import sys

# print(sys.executable)
usingJep = 'java' in sys.executable  
   # who is the caller: Java or something else (Python)

if not usingJep:
    import matplotlib
    import matplotlib.pyplot as plt
    import numpy as np
    print('Running graphs.py from Python')
else:
    print('Running graphs.py from Java')


x = np.linspace(-5, 2, 100)

y1 = x**3 + 5*x**2 + 10
y2 = 3*x**2 + 10*x
y3 = 6*x + 10

fig, ax = plt.subplots()

ax.plot(x, y1, color="blue", label="y(x)")
ax.plot(x, y2, color="red", label="y'(x)")
ax.plot(x, y3, color="green", label="y''(x)")

ax.set_xlabel("x")
ax.set_ylabel("y")
ax.legend()

# only show the graph if running from Python
if not usingJep:
    plt.show()

'''
 if this script is called from JEP then the plot 
 is saved to a file after the end of this script, 
 and then loaded by Java
'''
