简体   繁体   English

使用 matplotlib 查找图形的最低点

[英]Finding the lowest point of a graph using matplotlib

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假设我有 2 个数组,一个具有用于图形斜率的一系列值,另一个用于绘制它们的卡方值,生成以下图像

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 (因为对于此示例,每个值有 50 个值,但如果我有 100 或 1000 个值,则需要筛选更多数据)对于此示例,斜率接近 -2 并且最低卡方约为 20K 抱歉,我是 matplot 的新手,是的,这是一个班级项目

Let's consider your curves are stored in a NumPy array.让我们考虑您的曲线存储在 NumPy 数组中。 If they are in a list, you can turn them into a NumPy array with all_chi2 = np.array(all_chi2) .如果它们在列表中,您可以使用all_chi2 = np.array(all_chi2)将它们转换为 NumPy 数组。 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.现在你有你的all_chi2数组,比如, m行和n列, m是 chi 向量中的点数, n是曲线数。

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) .因为all_chi2是一个二维数组,您正在寻找这个矩阵(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.好了,您可以自动从图表中提取您确定的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM