首页 > 解决方案 > 使用 matplotlib 查找图形的最低点

问题描述

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

plt.figure(figsize=(8,6))
plt.plot(maybe_slopes, chi2, c = 'grey')
plt.grid(True)

斜率与卡方

斜率与卡方

如何在不必探索整个参数网格的情况下找到与最小卡方对应的斜率?(因为对于这个例子,每个有 50 个值,但如果我有 100 或 1000 个值,则需要筛选更多数据)对于这个例子,斜率接近 -2 最低卡方约为 20K 抱歉,我是 matplot 新手,是的,这是一个课堂项目

标签: pythonmatplotlib

解决方案


让我们考虑一下您的曲线存储在 NumPy 数组中。如果它们在列表中,您可以将它们转换为带有all_chi2 = np.array(all_chi2). 现在你有你的数组all_chi2,比如说, m行和n列,其中m是 chi 向量中的点数,并且n是曲线数。

因为all_chi2是二维数组,所以要找这个矩阵的最小值的坐标(m_min, n_min)。这可以通过

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)

在那里,您可以自动从图表中提取您精确定位的值。


推荐阅读