python - 使用 matplotlib 查找图形的最低点
问题描述
假设我有 2 个数组,一个具有图形斜率的一系列值,另一个用于绘制它们的卡方值,生成以下图像
plt.figure(figsize=(8,6))
plt.plot(maybe_slopes, chi2, c = 'grey')
plt.grid(True)
斜率与卡方
如何在不必探索整个参数网格的情况下找到与最小卡方对应的斜率?(因为对于这个例子,每个有 50 个值,但如果我有 100 或 1000 个值,则需要筛选更多数据)对于这个例子,斜率接近 -2 最低卡方约为 20K 抱歉,我是 matplot 新手,是的,这是一个课堂项目
解决方案
让我们考虑一下您的曲线存储在 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)
在那里,您可以自动从图表中提取您精确定位的值。
推荐阅读
- python - 复制嵌套列表并用连续的数字填充
- django - Django Queryset 加载时间过长
- bash - Ctrl+C 在 Git Bash 2.20.1 中不能与 gradle 一起使用
- shell - 我无法从其他函数 shell 脚本中的函数获取值
- macos - 有没有办法在没有 UI 的情况下检查下载(Mac / Unix)
- javascript - 当使用带有条件的 $convert 和错误输出仍然是 NaN
- sql - 如何在 SQL 中使用 PIVOT 获得实际结果?
- spring - Spring Environment 类 - 使用动态键加载属性值
- laravel - 如何通过 1 laravel 更新字段的值
- c - 函数参数使用不一致不会导致编译器警告