Search code examples
pythonfipy

datamin and datamax in Viewer() did not work


I used the above code in the fipy, but the range of color bar is always not changed( always showed [-1,1]). My version is Python 3.6.7 and matlablib 3.1.1.

#create a viewer to see the results
    if __name__ == '__main__':
        viewer = Viewer(vars=psi, datamin=-0.01, datamax=0)
        viewer.plot()

I tried the following: 1, I changed the code in "C:\Users\Lenovo\fipy\examples\diffusion", for example, changed "datamin=-0.01, datamax=0". is not work. 2, directly used "MatplotlibViewer". is not work. 3, used “GnuplotViewer". Can not install in Python.


Solution

  • Currently, there is a bug in FiPy such that the datamin and datamax values are not used when rendering the colorbar for 2D plots. The issue is described here and here. There is a workaround, however.

    This example renders a plot that should have three cells with max value above 3, but is cut off at 3.

    from fipy import Grid2D, CellVariable, Viewer
    from fipy.viewers.matplotlibViewer.matplotlibViewer import _ColorBar
    
    m = Grid2D(nx=3, ny=3)
    
    v = CellVariable(mesh=m)
    v[:] = m.x * m.y
    
    vi = Viewer(v, colorbar=None, datamin=0.0, datamax=3.0)
    vi.colorbar = _ColorBar(viewer=vi, vmin=0.0, vmax=3.0)
    
    vi.plot()
    
    input('stopped')
    

    It's necessary to use the datamin and datamax arguments and set the colorbar argument to None and then add the colorbar with the correct vmin and vmax values as these aren't being set correctly in FiPy.