Skip to main content
 首页 » 编程设计

python之单个查看器中的两个等高线图之Python FiPy

2024年11月01日7softidea

我正在尝试求解在给定域内几何变化的两个自变量。我想在单个查看器显示中绘制它们的方差。如何获得两个不同的等高线图,每个用于单个查看器框中的自变量?我对双轮廓使用了以下代码,但无法为两个变量(在我的例子中是 phasegamma 和 phasesigma)获得不同的轮廓。请建议如何纠正它或任何其他可能的方法来在一个图中获得两个等高线。

import pylab 
class PhaseViewer(Matplotlib2DGridViewer): 
   def __init__(self, phasesigma, phasegamma, title = None, limits ={}, **kwlimits): 
       self.phasesigma = phasesigma 
       self.contour1 = None 
       self.phasegamma = phasegamma 
       self.contour2 = None 
 
       Matplotlib2DGridViewer.__init__(self, vars=(1-phasegamma-phasesigma),title=title,cmap=pylab.cm.hot,limits ={}, **kwlimits) 
   def _plot(self): 
       Matplotlib2DGridViewer._plot(self) 
 
       if self.contour1 is not None or self.contour2 is not None: 
          for Ccr in self.contour1.collections: 
                  Ccr.remove() 
          for Cni in self.contour1.collections: 
                  Cni.remove()     
       mesh = self.phasesigma.getMesh() 
       mesh2 = self.phasegamma.getMesh() 
       shape = mesh.getShape() 
       shape2 = mesh2.getShape() 
       x, y = mesh.getCellCenters() 
       z = self.phasesigma.getValue() 
       x, y, z = [a.reshape(shape, order="FORTRAN") for a in (x, y, z)] 
       self.contour1 = pylab.contour(x, y, z, (0.5,)) 
       l, m = mesh1.getCellCenters() 
       w = self.phasegamma.getValue() 
       l, m, w = [b.reshape(shape, order ="FORTRAN") for b in (l, m, w)] 
       self.contour2 = pylab.contour(l, m, w, (0.5,)) 
       raw_input("check2") 
 
viewer = PhaseViewer(phasesigma=phasesigma, phasegamma=phasegamma,\ 
            title = r"%s & %s" % (phasegamma.name, phasesigma.name), datamin=0., datamax=1.) 

除了导入错误: viewer = MultiViewer(viewers=(Viewer(vars=phasesigma,datamin=0.,datamax=1),Viewer(vars=phasegamma,datamin=0.,datamax=1.)))

请您参考如下方法:

我刚看到这个,希望它对你仍然有用。我不确定为什么你的版本不起作用,尽管我通常发现 pylab 的工作水平太高并且自动做太多事情。

我基于 Matplotlib2DContourViewer 进行以下操作,它似乎可以满足您的要求:

class PhaseViewer(Matplotlib2DGridViewer): 
   def __init__(self, phasesigma, phasegamma, title = None, limits ={}, **kwlimits): 
       self.phasesigma = phasesigma 
       self.contour1 = None 
       self.phasegamma = phasegamma 
       self.contour2 = None 
       self.number = 10 
       self.levels = None 
 
       Matplotlib2DGridViewer.__init__(self, vars=(1-phasegamma-phasesigma),title=title,cmap=pylab.cm.hot,limits ={}, **kwlimits) 
   def _plot(self): 
       Matplotlib2DGridViewer._plot(self) 
 
       if hasattr(self, "_contourSet"): 
           for countourSet in self._contourSet: 
               for collection in ccontourSet.collections: 
                   try: 
                       ix = self.axes.collections.index(collection) 
                   except ValueError, e: 
                       ix = None 
 
                   if ix is not None: 
                       del self.axes.collections[ix] 
       self._contourSet = [] 
 
       for var in (self.phasesigma, self.phasegamma): 
           mesh = var.mesh 
           x, y = mesh.cellCenters 
           z = var.value 
 
           xmin, ymin = mesh.extents['min'] 
           xmax, ymax = mesh.extents['max'] 
 
           from matplotlib.mlab import griddata 
 
           xi = fp.numerix.linspace(xmin, xmax, 1000) 
           yi = fp.numerix.linspace(ymin, ymax, 1000) 
           # grid the data. 
           zi = griddata(x, y, z, xi, yi, interp='linear') 
 
 
           zmin, zmax = self._autoscale(vars=[var], 
                                        datamin=self._getLimit(('datamin', 'zmin')), 
                                        datamax=self._getLimit(('datamax', 'zmax'))) 
 
           self.norm.vmin = zmin 
           self.norm.vmax = zmax 
 
           if self.levels is not None: 
               levels = self.levels 
           else: 
               levels = fp.numerix.arange(self.number + 1) * (zmax - zmin) / self.number + zmin 
 
 
           self._contourSet.append(self.axes.contour(xi, yi, zi, levels=levels, cmap=self.cmap)) 
 
       self.axes.set_xlim(xmin=self._getLimit('xmin'), 
                          xmax=self._getLimit('xmax')) 
 
       self.axes.set_ylim(ymin=self._getLimit('ymin'), 
                          ymax=self._getLimit('ymax')) 
 
       if self.colorbar is not None: 
               self.colorbar.plot()