Python Snippets: Difference between revisions

From Fluids Wiki
Jump to navigation Jump to search
(Created page with " === Package abbreviations === Unless otherwise specified, the snippets use the following conventions for package abbreviations <syntaxhighlight lang="python"> import matplo...")
 
Line 16: Line 16:
While there are options for shifting it exponent around to fit better, I find it looks a lot better to simply place it in the ylabel for the colour bar.
While there are options for shifting it exponent around to fit better, I find it looks a lot better to simply place it in the ylabel for the colour bar.
This also provides a nice opportunity to add units!
This also provides a nice opportunity to add units!
=== Function Definition ===
First, we'll define a function, because it keeps things tidy.
To use it, simply pass the handle for a colour bar to the function <code>ScientificCbar</code>.
It has some option arguments that may be useful.
# <code>units</code> (string) Simply set this argument to the desired string to have it included in the colour bar. The default is the empty string.
# <code>orientation</code> (string) Specifies whether or not the colour bar should be horizontal or vertical. Default is <code>'vertical'</code>.
# <code>centre</code> (boolean) If <code>True</code> (the default), will forcibly centre the colour bar around 0.


'''Notes:'''
'''Notes:'''
# Units are specified in line 20. Simply set to the empty string if you don't want units included.
# If you need more significant digits, modify the format string <code>{0:.2g}</code> (lines 26 and 31) by changing the <code>2</code> to be the desired number of digits
# If you need more significant digits, modify the format string <code>{0:.2g}</code> by changing the <code>2</code> to be the desired number of digits
# This setting only permits 5 ticks along the colour bar: this is to avoid clutter while giving basic information on scaling. If you need / want more, simply modify line 13.
# This setting only permits 5 ticks along the colour bar: this is to avoid clutter while giving basic information on scaling. If you need / want more, simply modify line 9.
 
<syntaxhighlight lang="python" highlight="13, 26, 31" line>
 
import matplotlib as mpl
import numpy as np
 
def ScientificCbar(cbar,
        units='',
        orientation='vertical',
        centre=True):
 
    # If requested, centre the colour bar
    if centre:
        cv = np.max(np.abs(cbar.mappable.get_clim()))
        cbar.mappable.set_clim(-cv, cv)
 
    # Limit the number of ticks on the colour bar
    tick_locator = mpl.ticker.MaxNLocator(nbins=5)
    cbar.locator = tick_locator
    cbar.update_ticks()
    ticks = cbar.get_ticks()
 
    # Re-scale the values to avoid the poorly-placed exponent
    #  above the colour bar
    scale = np.log10(np.max(np.abs(ticks)))
    scale = np.floor(scale)
 
    # Instead, simply add a ylabel to the colour bar giving the scale.
    if orientation == 'vertical':
        if scale != 0.:
            cbar.ax.set_yticklabels(["{0:.2g}".format(tick/(10**scale)) for tick in ticks])
        cbar.ax.set_ylabel('$\\times10^{' + '{0:d}'.format(int(scale)) + '}$ ' + units,
                rotation = '-90', labelpad=10)
    elif orientation == 'horizontal':
        if scale != 0.:
            cbar.ax.set_xticklabels(["{0:.2g}".format(tick/(10**scale)) for tick in ticks])
        cbar.ax.set_xlabel('$\\times10^{' + '{0:d}'.format(int(scale)) + '}$ ' + units,
                rotation = '0', labelpad=10)


<syntaxhighlight lang="python" highlight="9,20,24" line>
</syntaxhighlight>
 
=== Sample Usage ===
 
<syntaxhighlight lang="python" line>
# Create colour bar in usual fashion
# Create colour bar in usual fashion
#  the second and third line are useful
#  the second and third line are useful
Line 30: Line 81:
cbar.solids.set_edgecolor("face")
cbar.solids.set_edgecolor("face")


# Limit the number of ticks on the colour bar
ScientificCbar(cbar)
tick_locator = mpl.ticker.MaxNLocator(nbins=5)
cbar.locator = tick_locator
cbar.update_ticks()
ticks = cbar.get_ticks()
 
# Re-scale the values to avoid the poorly-placed exponent
#  above the colour bar
scale = np.log10(np.max(np.abs(ticks)))
scale = np.floor(scale)
 
# Specify units
units = 's$^{-1}$'


# Instead, simply add a ylabel to the colour bar giving the scale.
if scale != 0.:
    cbar.ax.set_yticklabels(["{0:.2g}".format(tick/(10**scale)) for tick in ticks])
    cbar.ax.set_ylabel('$\\times10^{' + '{0:d}'.format(int(scale)) + '}$ ' + units,
            rotation = '-90', va='bottom')
</syntaxhighlight>
</syntaxhighlight>

Revision as of 16:20, 16 August 2019


Package abbreviations

Unless otherwise specified, the snippets use the following conventions for package abbreviations

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np


Improving exponential notation in colour bars

The default behaviour for handling exponents in colour bars isn't the prettiest, and can sometimes overlap the plot window itself. While there are options for shifting it exponent around to fit better, I find it looks a lot better to simply place it in the ylabel for the colour bar. This also provides a nice opportunity to add units!


Function Definition

First, we'll define a function, because it keeps things tidy. To use it, simply pass the handle for a colour bar to the function ScientificCbar. It has some option arguments that may be useful.

  1. units (string) Simply set this argument to the desired string to have it included in the colour bar. The default is the empty string.
  2. orientation (string) Specifies whether or not the colour bar should be horizontal or vertical. Default is 'vertical'.
  3. centre (boolean) If True (the default), will forcibly centre the colour bar around 0.

Notes:

  1. If you need more significant digits, modify the format string {0:.2g} (lines 26 and 31) by changing the 2 to be the desired number of digits
  2. This setting only permits 5 ticks along the colour bar: this is to avoid clutter while giving basic information on scaling. If you need / want more, simply modify line 13.
import matplotlib as mpl
import numpy as np

def ScientificCbar(cbar, 
        units='', 
        orientation='vertical', 
        centre=True):

    # If requested, centre the colour bar
    if centre:
        cv = np.max(np.abs(cbar.mappable.get_clim()))
        cbar.mappable.set_clim(-cv, cv)

    # Limit the number of ticks on the colour bar
    tick_locator = mpl.ticker.MaxNLocator(nbins=5)
    cbar.locator = tick_locator
    cbar.update_ticks()
    ticks = cbar.get_ticks()

    # Re-scale the values to avoid the poorly-placed exponent
    #   above the colour bar
    scale = np.log10(np.max(np.abs(ticks)))
    scale = np.floor(scale)

    # Instead, simply add a ylabel to the colour bar giving the scale.
    if orientation == 'vertical':
        if scale != 0.:
            cbar.ax.set_yticklabels(["{0:.2g}".format(tick/(10**scale)) for tick in ticks])
        cbar.ax.set_ylabel('$\\times10^{' + '{0:d}'.format(int(scale)) + '}$ ' + units,
                rotation = '-90', labelpad=10)
    elif orientation == 'horizontal':
        if scale != 0.:
            cbar.ax.set_xticklabels(["{0:.2g}".format(tick/(10**scale)) for tick in ticks])
        cbar.ax.set_xlabel('$\\times10^{' + '{0:d}'.format(int(scale)) + '}$ ' + units,
                rotation = '0', labelpad=10)

Sample Usage

# Create colour bar in usual fashion
#   the second and third line are useful
#   for pdf outputs
cbar = plt.colorbar(plot_handle, ...)
cbar.solids.set_rasterized(True)
cbar.solids.set_edgecolor("face")

ScientificCbar(cbar)