Reputation: 37
Say I have 2 arrays, one with a range of values for the slope of a graph, and another one for the chi squared values graphing them produces the following image
plt.figure(figsize=(8,6))
plt.plot(maybe_slopes, chi2, c = 'grey')
plt.grid(True)
Slope vs Chi squared
How can I find the slope corresponding to the minimum chi square without having to explore the whole grid of parameters? (since for this examples, there are 50 values per, but if I had 100 or 1000 values, there is more data to sift through) For this example, the slope is close to -2 And the lowest chi squared is around 20K Sorry, I'm new with matplot, and yes this is for a class project
Upvotes: 2
Views: 1378
Reputation: 2630
Let's consider your curves are stored in a NumPy array. If they are in a list, you can turn them into a NumPy array with all_chi2 = np.array(all_chi2)
. Now you have your array of all_chi2
with, say, m
rows and n
columns, with m
being the number of points in the chi vector, and n
being the number of curves.
Because all_chi2
is a 2-dimensional array, you are looking for the coordinate of the minimum value of this matrix (m_min, n_min)
. This can be done with
import numpy as np
# first, find the index of maximum on the unraveled matrix
arg_min = np.argmin(all_chi2)
# then find back the 2d indexes
m_min, n_min = np.unravel_index(arg_min, allchi2.shape)
There you go, you can extract the values that you pinpointed from the graph automatically.
Upvotes: 1