python - 将级别值存储在等值线图中

标签 python matplotlib scipy

我来自这个问题Find most distant points in contour curve给出的答案允许我使用下面的代码生成如下所示的等高线图。

contour curves

将 scipy 导入为 sp 从 scipy 导入统计数据 将 pylab 导入为 pl

x = [1.81,1.715,1.78,1.613,1.629,1.714,1.62,1.738,1.495,1.669,1.57,1.877,1.385,2.129, \
     2.016,1.606,1.444,2.103,1.397,1.854,1.327,1.377,1.798,1.684,2.186,2.079,1.32, \
     1.452,2.272,1.313,1.762,2.308,2.285,2.328,2.288,2.345,2.237,2.078,2.057,1.505, \
     2.595,2.176,2.501,0.942,2.424,2.49,2.65,1.303,2.43,2.241,0.897,1.731,2.464,1.638, \
     0.867,2.392,3.248,2.608,2.733,0.745,2.715,3.078,2.571,0.771,1.071,2.574,3.343, \
     2.835,2.629,3.421,0.642,2.571,2.698,0.595,2.912,0.563,2.832,2.636,3.149,2.522, \
     0.836,0.894,0.447,1.304,1.132,2.488,3.363,2.961,1.317,2.387,0.036,2.199,0.356, \
     3.036,2.103,2.894,-0.097,0.069,2.688,-0.083,0.653,3.247,3.045,3.197,2.963,2.473, \
     2.571,3.333,3.009,1.281,3.257,3.116,2.673,2.901,2.903,2.634,-0.291,-0.29,0.212]
y = [0.924,0.915,0.914,0.91,0.909,0.905,0.905,0.893,0.886,0.881,0.873,0.873,0.844, \
     0.838,0.83,0.817,0.811,0.809,0.807,0.803,0.802,0.792,0.777,0.774,0.774,0.77,0.748, \
     0.746,0.742,0.734,0.729,0.726,0.722,0.677,0.676,0.672,0.635,0.62,0.62,0.608,0.605, \
     0.587,0.586,0.578,0.571,0.569,0.549,0.544,0.535,0.53,0.529,0.513,0.499,0.497, \
     0.496,0.496,0.49,0.486,0.482,0.476,0.474,0.473,0.471,0.47,0.459,0.444,0.438,0.435, \
     0.428,0.419,0.411,0.4,0.396,0.384,0.378,0.368,0.362,0.362,0.361,0.357,0.347,0.346, \
     0.344,0.33,0.322,0.319,0.318,0.305,0.296,0.296,0.289,0.288,0.288,0.288,0.287, \
     0.286,0.283,0.283,0.278,0.274,0.264,0.259,0.248,0.244,0.241,0.239,0.238,0.237, \
     0.23,0.222,0.221,0.218,0.214,0.212,0.207,0.205,0.196,0.19,0.182]

xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)

# Generate KDE
x1, y1 = sp.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = sp.vstack([x1.ravel(), y1.ravel()])
values = sp.vstack([x, y])
kernel = stats.gaussian_kde(values)
kde = sp.reshape(kernel(positions).T, x1.shape)

# plotting
CS = pl.contour(x1,y1,kde)
pl.clabel(CS, inline=1, fontsize=10, zorder=6)
pl.show()

这里 python 决定设置轮廓曲线的级别值。我知道我可以使用以下内容手动定义这些级别:

levels = np.arange(0.2, 1., 0.2)
CS = plt.contour(x1,y1,kde,levels)

但我想知道如何获取并存储 python 选择的这些值,以便最终得到一个列表,在上面显示的图像的情况下,该列表如下所示:

levels = [0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1]

最佳答案

级别存在于 ContourSet 对象上:

CS = pl.contour(x1,y1,kde)
levels = CS.levels

关于python - 将级别值存储在等值线图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19865892/

相关文章:

python - 带有python native 日期时间类型而不是时间戳的pandas to_dict

python-3.x - 带有圆角的 Seaborn 条形图

python - sin(y) 导致 "can' t 将表达式转换为 float"错误

python - 访问 UnivariateSpline 曲线上的值

python - 如何让 Pandas 在非均匀 x 网格上执行滚动平均

python - 使用掩蔽数组的一行计算数组行的平均值

Python:如何从 Flask 中的 URL 获取多个变量?

python - groupby.value_counts() 之后的 pandas reset_index

python - Python中重定向后获取页面的url

python - 使用 matplot 库绘制图形时,直方图的哪一部分引用二维数组的行?