On 2012/11/26 11:37 AM, TP wrote:
> On Thursday, November 22, 2012 23:51:08 TP wrote:
>> Thus it seems to me that my dummy example given in the previous post covers
>> exactly the problem encountered in my real-world imshow function.
>>
>> Is there a memory-efficient workaround in my dummy example (instead of
>> increasing N)?
>
> I have modified LinearSegmentedColormap so as to solve my problem. The
> difference is that I do not create an huge array in my test case, but instead
> I
> interpolate linearly in the colormap. This is a quick and dirty code that
> does work in my case, but which does not deal with all cases (no management of
> transparency, no discontinuity in the colormap, etc.)
I'm glad you found a solution, but my sense is that the problem is that
you are trying to make the colormap do the work of the norm. The
colormap is just a set of discrete colors, with a linear mapping to the
0-1 scale (apart from the special under, over, and invalid values). The
norm is for mapping your data to those colors, however you like, by
mapping your data to the 0-1 range (again with possible under, over, and
invalid values). Did you consider making a custom norm instead of
modifying the colormap?
Eric
>
> #####################
> from __future__ import division
> from pylab import *
> from matplotlib.colors import LinearSegmentedColormap
> from matplotlib.collections import CircleCollection
>
> from scipy.interpolate import interp1d
>
>
> class ContinuousLinearSegmentedColormap( LinearSegmentedColormap ):
>
> def __init__(self, name, segmentdata, gamma=1.0):
>
> LinearSegmentedColormap.__init__( self
> , name, segmentdata, gamma = gamma )
>
> def _init(self):
>
> self.N = len( self._segmentdata['red'] )
> self._lut = np.ones((self.N, 5), np.float)
> for i in range( self.N ):
> self._lut[i, 0] = self._segmentdata['red'][i][0]
> # 2 because I do not manage discontinuities in color
> self._lut[i, 1] = self._segmentdata['red'][i][2]
> self._lut[i, 2] = self._segmentdata['green'][i][2]
> self._lut[i, 3] = self._segmentdata['blue'][i][2]
>
> self._isinit = True
>
>
> def __call__(self, X, alpha=None, bytes=False):
>
> if not self._isinit: self._init()
> mask_bad = None
> if not cbook.iterable(X):
> vtype = 'scalar'
> xa = np.array([X])
> else:
> vtype = 'array'
> xma = ma.array(X, copy=False)
> mask_bad = xma.mask
> xa = xma.data.copy() # Copy here to avoid side effects.
> del xma
>
> lut = self._lut.copy()
> rgba = np.empty(shape=xa.shape+(4,), dtype=lut.dtype)
>
> # We construct interpolation functions.
> fred = interp1d( lut[:,0], lut[:,1])
> fgreen = interp1d( lut[:,0], lut[:,2])
> fblue = interp1d( lut[:,0], lut[:,3])
>
> rgba[:,3] = 1 # alpha=1 for the time being
> for i in range( xa.shape[0] ):
> rgba[i,0] = fred( xa[i] )
> rgba[i,1] = fgreen( xa[i] )
> rgba[i,2] = fblue( xa[i] )
>
> if vtype == 'scalar':
> rgba = tuple(rgba[0,:])
> return rgba
>
>
> ioff()
>
>
> large_value = 257 # blue above this value
> large_value = 258 # black above this value
> large_value = 1e8
>
> cdict = { 'blue': [(0.0, 0.0, 0.0)
> , (2*1/large_value, 1, 1)
> , (1.0, 1.0, 1.0)]
> , 'green': [(0.0, 0.0, 0.0)
> , (2*1/large_value, 0, 0)
> , (1.0, 1.0, 1.0)]
> , 'red': [(0.0, 0.0, 0.0)
> , (2*1/large_value, 0, 0)
> , (1.0, 1.0, 1.0)] }
>
> measures= array( [[ 0.2, 0.3, 0],
> [ 0.3, 0.4, 2],
> [ 0.5, 0.6, large_value]] )
>
> cmap = ContinuousLinearSegmentedColormap( "cmap foobar"
> , cdict
> )
>
> fig = figure()
> axes = fig.add_subplot(111)
> ec = CircleCollection( [80]
> , offsets = measures[:,:2]
> , transOffset = axes.transData
> )
>
> ec.set_array( measures[:,2] )
> ec.set_cmap( cmap )
> axes.add_collection( ec )
>
> show()
> #####################
>
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> Matplotlib-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
Matplotlib-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users