Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find the global minima from a matplotlib graph?

import numpy as np
import matplotlib.pyplot as plt

x = [1 ,2, 3, 4, 5, 6, 7, 8, 9]
y = [ 3,5, 1, 9,  3,  2, 10,  7,  8]
plt.plot(x, y)

#for global minima
minpos = y.index(min(y))
plt.plot(x[minpos],min(y), 'go', label="Minima")
plt.show()

I have two arrays x and y. Here I’ve plotted them using Matplotlib and found the global minima using this simple logic. Here is the output that I’m getting:

Output

After that I’ve smoothen the graph BSpline

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from scipy.interpolate import make_interp_spline, BSpline

# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(min(x), max(x), 100) 

spl = make_interp_spline(x, y, k=2)  # type: BSpline
power_smooth = spl(xnew)
plt.plot(x[minpos],min(y), 'go', label="Minima")
plt.plot(xnew, power_smooth)
plt.show()

Output

Now my position of the global minima has changed and that simple logic will not work here. I want to know how I can find the global minima from a graph in this case

>Solution :

Use numpy.argmin on power_smooth:

minpos = np.argmin(power_smooth)
min_x = xnew[minpos]
min_y = power_smooth[minpos]

plt.plot(min_x, min_y, 'go', label="Minima")

Output:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading