Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python matplotlib colorbar scientific notation base

I am trying to customise a colorbar on my matpllotlib contourf plots. Whilst I am able to use scientific notation I am trying to change the base of the notation - essentially so that my ticks would be in the range of (-100,100) rather than (-10,10).

For example, this produces a simple plot...

import numpy as np
import matplotlib.pyplot as plt

z = (np.random.random((10,10)) - 0.5) * 0.2

fig, ax = plt.subplots()
plot = ax.contourf(z)
cbar = fig.colorbar(plot)

cbar.formatter.set_powerlimits((0, 0))
cbar.update_ticks()

plt.show()

like so:

enter image description here

However, I would like the label above the colorbar to be 1e-2 and the numbers to range from -10 to 10.

How would I go about this?

like image 407
John Avatar asked Apr 10 '17 13:04

John


People also ask

How do you enter scientific notation in matplotlib?

To show decimal places and scientific notation on the axis of a matplotlib, we can use scalar formatter by overriding _set_format() method.

How do I get rid of matplotlib scientific notation?

To prevent scientific notation, we must pass style='plain' in the ticklabel_format method.

How do you change the scale Colorbar in Python?

Use the vmin and vmax Parameter to Set the Range of Colorbar in Python. The vmin and vmax parameters can be used to specify the scale for mapping color values. These parameters work with the object, which uses colormaps. It can be used to control the range of the colorbar in matplotlib.

What is the matplotlib Pyplot function that plots a Colorbar on the side of a plot?

The colorbar() function in pyplot module of matplotlib adds a colorbar to a plot indicating the color scale.


1 Answers

A possible solution can be to subclass the ScalarFormatter and fix the order of magnitude as in this question: Set scientific notation with fixed exponent and significant digits for multiple subplots

You would then call this formatter with the order of magnitude as the argument order, OOMFormatter(-2, mathText=False). mathText is set to false to obtain the notation from the question, i.e. enter image description here while setting it to True, would give enter image description here.

You can then set the formatter to the colorbar via the colorbar's format argument.

import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
import matplotlib.ticker

class OOMFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, order=0, fformat="%1.1f", offset=True, mathText=True):
        self.oom = order
        self.fformat = fformat
        matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
    def _set_order_of_magnitude(self):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin=None, vmax=None):
        self.format = self.fformat
        if self._useMathText:
             self.format = r'$\mathdefault{%s}$' % self.format


z = (np.random.random((10,10)) - 0.5) * 0.2

fig, ax = plt.subplots()
plot = ax.contourf(z)
cbar = fig.colorbar(plot, format=OOMFormatter(-2, mathText=False))

plt.show()

enter image description here

For matplotlib versions < 3.1 the class needs to look like this:

class OOMFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, order=0, fformat="%1.1f", offset=True, mathText=True):
        self.oom = order
        self.fformat = fformat
        matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
    def _set_orderOfMagnitude(self, nothing):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin, vmax):
        self.format = self.fformat
        if self._useMathText:
            self.format = '$%s$' % matplotlib.ticker._mathdefault(self.format)
like image 109
ImportanceOfBeingErnest Avatar answered Sep 30 '22 02:09

ImportanceOfBeingErnest