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

2024-05-19 00:00:36 发布

您现在位置: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编写库代码一无所知,我非常感谢您的帮助!在


Tags: 数据对象函数代码fromimport绘图library
1条回答
网友
1楼 · 发布于 2024-05-19 00:00:36

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

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

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()

enter image description here

相关问题 更多 >

    热门问题