从 Python 中的对象获取数据数组

Get data array from object in Python

我正在使用 library,它在给定对象 k 的情况下生成 3 个图。

我需要计算生成这些图的数据点 (x,y,z),但问题是这些图来自 k.

的函数

我使用的库是 pyKrigingthis 是他们的 github 存储库。

他们的示例 code 的简化版本是:

import pyKriging  
from pyKriging.krige import kriging  
from pyKriging.samplingplan import samplingplan

sp = samplingplan(2)  
X = sp.optimallhc(20)

testfun = pyKriging.testfunctions().branin  
y = testfun(X)

k = kriging(X, y, testfunction=testfun, name='simple')   
k.train()
k.plot()

可以找到完整的代码、注释和输出 here

总而言之,我正在尝试获取生成这些图的 numpy 数组,以便我可以创建符合我的格式样式的图。

我不了解 Python 中的库代码,非常感谢您的帮助!

没有生成绘图的单个数据数组。相反,许多用于绘图的数组是在 kriging plot 函数内部生成的。
将填充轮廓更改为线条轮廓当然不是样式选项。因此需要使用原始绘图函数中的代码。

一个选项是子类化 kriging 并实现自定义绘图函数(我们称之为 myplot)。在这个函数中,可以使用 contour 而不是 contourf。当然,也可以完全根据自己的需要进行更改。

import pyKriging  
from pyKriging.krige import kriging  
from pyKriging.samplingplan import samplingplan
import numpy as np
import matplotlib.pyplot as plt

class MyKriging(kriging):
    def __init__(self,*args,**kwargs):
        kriging.__init__(self,*args,**kwargs)
    def myplot(self,labels=False, show=True, **kwargs):
        fig = plt.figure(figsize=(8,6))
        # Create a set of data to plot
        plotgrid = 61
        x = np.linspace(self.normRange[0][0], self.normRange[0][1], num=plotgrid)
        y = np.linspace(self.normRange[1][0], self.normRange[1][1], num=plotgrid)
        X, Y = np.meshgrid(x, y)
        # Predict based on the optimized results
        zs = np.array([self.predict([xi,yi]) for xi,yi in zip(np.ravel(X), np.ravel(Y))])
        Z = zs.reshape(X.shape)
        #Calculate errors
        zse = np.array([self.predict_var([xi,yi]) for xi,yi in zip(np.ravel(X), np.ravel(Y))])
        Ze = zse.reshape(X.shape)

        spx = (self.X[:,0] * (self.normRange[0][1] - self.normRange[0][0])) + self.normRange[0][0]
        spy = (self.X[:,1] * (self.normRange[1][1] - self.normRange[1][0])) + self.normRange[1][0]

        contour_levels = kwargs.get("levels", 25)
        ax = fig.add_subplot(222)
        CS = plt.contour(X,Y,Ze, contour_levels)
        plt.colorbar()
        plt.plot(spx, spy,'or')

        ax = fig.add_subplot(221)
        if self.testfunction:
            # Setup the truth function
            zt = self.testfunction( np.array(zip(np.ravel(X), np.ravel(Y))) )
            ZT = zt.reshape(X.shape)
            CS = plt.contour(X,Y,ZT,contour_levels ,colors='k',zorder=2, alpha=0)

        if self.testfunction:
            contour_levels = CS.levels
            delta = np.abs(contour_levels[0]-contour_levels[1])
            contour_levels = np.insert(contour_levels, 0, contour_levels[0]-delta)
            contour_levels = np.append(contour_levels, contour_levels[-1]+delta)

        CS = plt.contour(X,Y,Z,contour_levels,zorder=1)
        plt.plot(spx, spy,'or', zorder=3)
        plt.colorbar()

        ax = fig.add_subplot(212, projection='3d')
        ax.plot_surface(X, Y, Z, rstride=3, cstride=3, alpha=0.4)
        if self.testfunction:
            ax.plot_wireframe(X, Y, ZT, rstride=3, cstride=3)
        if show:
            plt.show()



sp = samplingplan(2)  
X = sp.optimallhc(20)

testfun = pyKriging.testfunctions().branin  
y = testfun(X)

k = MyKriging(X, y, testfunction=testfun, name='simple')   
k.train()
k.myplot()