4

I want to generate a grid of plots, of several arrays, with positive and negative values, with log scale, sharing the same colorbar.

I've achieved the sharing part of the colorbar (using ImageGrid and common max and min values), and I know that I could get a logarithmic scale using LogNorm() on the imshow call in the case of only positive values. But given the presence of negative values, I would need a colorbar on symmetric logarithmic scale.

I have found what would be the solution on https://stackoverflow.com/a/7741317/1101750 , but running the sample code Yann provides gives me very different results, cleary wrong: Result of Yann example Reviewing the code, I'm not able to grasp what's going on.

In addition to that, I've discovered that on Matplotlib 1.2, scale.SymmetricalLogScale.SymmetricalLogTransform asks for a new argument not explained on the documentation (linscale, which looking at the code of other transforms I assume that leaving it as 1 is a safe value).

Is the easiest solution subclassing LogNorm?

Community
  • 1
  • 1
Sergi
  • 454
  • 1
  • 4
  • 21

2 Answers2

4

I've used a pretty simple recipe in the past to do exactly this, without the need to do any subclassing. matplotlib.colors.SymLogNorm provides most of the functionality you need, except that I've found it necessary to generate the tick marks by hand. Note that this solution uses matplotlib 1.3.0, and I may be using features that weren't available with 1.2.

def imshow_symlog(my_matrix, vmin, vmax, logthresh=5):
    img=imshow( my_matrix ,
                vmin=float(vmin), vmax=float(vmax),
                norm=matplotlib.colors.SymLogNorm(10**-logthresh) )

    maxlog=int(np.ceil( np.log10(vmax) ))
    minlog=int(np.ceil( np.log10(-vmin) ))

    #generate logarithmic ticks 
    tick_locations=([-(10**x) for x in xrange(minlog,-logthresh-1,-1)]
                    +[0.0]
                    +[(10**x) for x in xrange(-logthresh,maxlog+1)] )

    cb=colorbar(ticks=tick_locations)
    return img,cb
Aaron V
  • 6,596
  • 5
  • 28
  • 31
-1

Since 1.3 matplotlib has a SymLogNorm. http://matplotlib.org/api/colors_api.html#matplotlib.colors.SymLogNorm

tillsten
  • 14,491
  • 5
  • 32
  • 41