How to improve colorbar scaling?

I have a problem with the scaling of the numbers on a colorbar. The
problem occurs when the numbers used as colorbar labels need to be
scaled (i.e. by 1E3). The colorbar correctly puts the scaling value
on the top of the colorbar, but instead of of multiplying by a scale
factor, addition is used instead. See the attached script and figure
for example. In the example, it looks like the color scale goes from
0 to 2*5E3. At least, that's what I thought when I first looked at
it. Instead it means 5000 to 5002.

Is there anyway I can scale the colorbar labels by *multiplying* them
by a scaling factor instead of *adding* a scaling factor? That seems
more intuitive to me and those I work with.

Jeremy

colorbarExample.py (171 Bytes)

colorbarExample.pdf (15.2 KB)

I have a problem with the scaling of the numbers on a colorbar. The
problem occurs when the numbers used as colorbar labels need to be
scaled (i.e. by 1E3). The colorbar correctly puts the scaling value
on the top of the colorbar, but instead of of multiplying by a scale
factor, addition is used instead. See the attached script and figure
for example. In the example, it looks like the color scale goes from
0 to 2*5E3. At least, that's what I thought when I first looked at
it. Instead it means 5000 to 5002.

Is there anyway I can scale the colorbar labels by *multiplying* them
by a scaling factor instead of *adding* a scaling factor? That seems
more intuitive to me and those I work with.

Given the value range of 5000-5002, I doubt how using the scaling
factor improve your plot.

To disable the use of offset (+5000),

cb = fig.colorbar(pc)

# do not use offset
cb.formatter.set_useOffset(False)

cb.update_ticks()

If you do want to use scaling factor,

# to use scaling factor
cb.formatter.set_scientific(True)
cb.formatter.set_powerlimits((0,0))

cb.update_ticks()

This will only work with matplotlib v1.0.
In older versions, try to replace "update_ticks" with
"update_bruteforce" (but I'm not sure if this will work)

IHTH,

-JJ

···

On Fri, Aug 13, 2010 at 3:34 AM, Jeremy Conlin <jlconlin@...287...> wrote:

Jeremy

------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Thanks for your suggestions. I recognize that a range of 5000–5002 is
not much; it was used simply to illustrate my point.

I was able to turn the scaling off with set_useOffset(False). Is
there anyway to scale by multiplying instead of adding?

Thanks,
Jeremy

···

On Sun, Aug 15, 2010 at 10:49 PM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

On Fri, Aug 13, 2010 at 3:34 AM, Jeremy Conlin <jlconlin@...287...> wrote:

I have a problem with the scaling of the numbers on a colorbar. The
problem occurs when the numbers used as colorbar labels need to be
scaled (i.e. by 1E3). The colorbar correctly puts the scaling value
on the top of the colorbar, but instead of of multiplying by a scale
factor, addition is used instead. See the attached script and figure
for example. In the example, it looks like the color scale goes from
0 to 2*5E3. At least, that's what I thought when I first looked at
it. Instead it means 5000 to 5002.

Is there anyway I can scale the colorbar labels by *multiplying* them
by a scaling factor instead of *adding* a scaling factor? That seems
more intuitive to me and those I work with.

Given the value range of 5000-5002, I doubt how using the scaling
factor improve your plot.

To disable the use of offset (+5000),

cb = fig.colorbar(pc)

# do not use offset
cb.formatter.set_useOffset(False)

cb.update_ticks()

If you do want to use scaling factor,

# to use scaling factor
cb.formatter.set_scientific(True)
cb.formatter.set_powerlimits((0,0))

cb.update_ticks()

This will only work with matplotlib v1.0.
In older versions, try to replace "update_ticks" with
"update_bruteforce" (but I'm not sure if this will work)

IHTH,

Using the set_powerlimits method didn't help?

As far as I know, the current implementation does not allow a custom
scale factor.
But if the scale factor is power of 10 (10, 100, 1000, ...), I believe
using set_powerlimits method (as in my previous example, or some
variation) is good enough.

Let me know if it does not work or you have any other problem.
Regards,

-JJ

···

On Tue, Aug 17, 2010 at 3:17 AM, Jeremy Conlin <jlconlin@...287...> wrote:

On Sun, Aug 15, 2010 at 10:49 PM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

On Fri, Aug 13, 2010 at 3:34 AM, Jeremy Conlin <jlconlin@...287...> wrote:

I have a problem with the scaling of the numbers on a colorbar. The
problem occurs when the numbers used as colorbar labels need to be
scaled (i.e. by 1E3). The colorbar correctly puts the scaling value
on the top of the colorbar, but instead of of multiplying by a scale
factor, addition is used instead. See the attached script and figure
for example. In the example, it looks like the color scale goes from
0 to 2*5E3. At least, that's what I thought when I first looked at
it. Instead it means 5000 to 5002.

Is there anyway I can scale the colorbar labels by *multiplying* them
by a scaling factor instead of *adding* a scaling factor? That seems
more intuitive to me and those I work with.

Given the value range of 5000-5002, I doubt how using the scaling
factor improve your plot.

To disable the use of offset (+5000),

cb = fig.colorbar(pc)

# do not use offset
cb.formatter.set_useOffset(False)

cb.update_ticks()

If you do want to use scaling factor,

# to use scaling factor
cb.formatter.set_scientific(True)
cb.formatter.set_powerlimits((0,0))

cb.update_ticks()

This will only work with matplotlib v1.0.
In older versions, try to replace "update_ticks" with
"update_bruteforce" (but I'm not sure if this will work)

IHTH,

Thanks for your suggestions. I recognize that a range of 5000–5002 is
not much; it was used simply to illustrate my point.

I was able to turn the scaling off with set_useOffset(False). Is
there anyway to scale by multiplying instead of adding?

Thanks,
Jeremy

Using the set_powerlimits method didn't help?

I couldn't get set_powerlimits or set_scientific to change anything in
my colorbar scaling. If I used setOffset(False) then there was no
scaling; an improvement, but not ideal.

As far as I know, the current implementation does not allow a custom
scale factor.
But if the scale factor is power of 10 (10, 100, 1000, ...), I believe
using set_powerlimits method (as in my previous example, or some
variation) is good enough.

Unfortunately in my simple example (and in my real world case), the
scale factor is some number (i.e. 5) times a power of 10.

Am I missing something? I'm running matplotlib version 1.0.0.

Thanks,
Jeremy

import numpy
import matplotlib.pyplot as pyplot

a = 5000
b = 5002

M = (b-a)*numpy.random.random((5,5))+a

fig = pyplot.figure()
pc = pyplot.pcolor(M)

cbar = fig.colorbar(pc)
cbar.formatter.set_scientific(False)
cbar.formatter.set_powerlimits((0,2))
# cbar.formatter.set_useOffset(False)

cbar.update_ticks()

···

On Mon, Aug 16, 2010 at 6:13 PM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

Try

cbar.formatter.set_useOffset(False)
cbar.formatter.set_scientific(True)
cbar.formatter.set_powerlimits((0,2))

It gives me

offsetText -> "x 10^3"
and tick labels = ["5.0002", "5.0004",...]

which I believe is what you want?

In case you want a scaling factor other than some power of tens, I
guess the easiest way is to scale the image itself and then use
"annotate" command to put the offsetText.

For example,

import numpy
import matplotlib.pyplot as pyplot

a = 5000
b = 5002

M = (b-a)*numpy.random.random((5,5))+a

fig = pyplot.figure()
pc = pyplot.pcolor(M/5000)

cbar = fig.colorbar(pc)

cbar.formatter.set_useOffset(False)

cbar.ax.annotate(r"\\times 5000",
                 (0.5, 1), xytext=(0, 5),
                 xycoords="axes fraction", textcoords="offset points")

cbar.update_ticks()

-JJ

···

On Tue, Aug 17, 2010 at 11:06 PM, Jeremy Conlin <jlconlin@...287...> wrote:

On Mon, Aug 16, 2010 at 6:13 PM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

Using the set_powerlimits method didn't help?

I couldn't get set_powerlimits or set_scientific to change anything in
my colorbar scaling. If I used setOffset(False) then there was no
scaling; an improvement, but not ideal.

As far as I know, the current implementation does not allow a custom
scale factor.
But if the scale factor is power of 10 (10, 100, 1000, ...), I believe
using set_powerlimits method (as in my previous example, or some
variation) is good enough.

Unfortunately in my simple example (and in my real world case), the
scale factor is some number (i.e. 5) times a power of 10.

Am I missing something? I'm running matplotlib version 1.0.0.

Thanks,
Jeremy

import numpy
import matplotlib.pyplot as pyplot

a = 5000
b = 5002

M = (b-a)*numpy.random.random((5,5))+a

fig = pyplot.figure()
pc = pyplot.pcolor(M)

cbar = fig.colorbar(pc)
cbar.formatter.set_scientific(False)
cbar.formatter.set_powerlimits((0,2))
# cbar.formatter.set_useOffset(False)

cbar.update_ticks()

Yes that is exactly what I want. Thanks for being persistent in
getting that through my thick skull.

Jeremy

···

On Wed, Aug 18, 2010 at 10:11 PM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

Try

cbar.formatter.set_useOffset(False)
cbar.formatter.set_scientific(True)
cbar.formatter.set_powerlimits((0,2))

It gives me

offsetText -> "x 10^3"
and tick labels = ["5.0002", "5.0004",...]