psy_simple.colors module

colors module of the psyplot package.

This module contains some additional color maps and the show_colormaps function to visualize available colormaps.

Classes:

FixedBoundaryNorm(boundaries, ncolors[, ...])

Bug fixing Norm with same functionality as matplotlibs BoundaryNorm

FixedColorMap(name, segmentdata[, N, gamma])

Bug fixing colormap with same functionality as matplotlibs colormap

Functions:

get_cmap(name[, lut])

Returns the specified colormap.

show_colormaps([names, N, show, use_qt])

Function to show standard colormaps from pyplot

class psy_simple.colors.FixedBoundaryNorm(boundaries, ncolors, clip=False, *, extend='neither')[source]

Bases: matplotlib.colors.BoundaryNorm

Bug fixing Norm with same functionality as matplotlibs BoundaryNorm

This class fixes a bug in the cartopy.mpl.geoaxes.GeoAxes.streamplot() for matplotlib version 1.5

Notes

To reproduce the error type:

>>> import cartopy.crs as ccrs
>>> import matplotlib.pyplot as plt
>>> import psyplot.project as psy
>>> import matplotlib.colors as mcol
>>> maps = psy.plot.mapvector(
...     'test-t2m-u-v.nc', name=[['u', 'v']], plot='stream',
...     lonlatbox='Europe', color='absolute')
>>> plotter = maps[0].plotter
>>> x, y, u, v = plotter.plot._get_data()
>>> maps.close(True, True)
>>> ax = plt.axes(projection=ccrs.PlateCarree())
>>> ax.set_extent(plotter.lonlatbox.lonlatbox, crs=ccrs.PlateCarree())
>>> m = ax.streamplot(
...     x, y, u, v, color=plotter.plot._kwargs['color'],
...     norm=mcol.BoundaryNorm(plotter.bounds.norm.boundaries,
...                            plotter.bounds.norm.Ncmap,
...                            plotter.bounds.norm.clip),
...                            density=[1.0, 1.0])

This raises in matplotlib.colors, line 1316, in matplotlib.colors.BoundaryNorm.__call__():

``ret = int(ret[0])  # assume python scalar``
MaskError: Cannot convert masked element to a Python int.
Parameters
  • boundaries (array-like) – Monotonically increasing sequence of at least 2 boundaries.

  • ncolors (int) – Number of colors in the colormap to be used.

  • clip (bool, optional) –

    If clip is True, out of range values are mapped to 0 if they are below boundaries[0] or mapped to ncolors - 1 if they are above boundaries[-1].

    If clip is False, out of range values are mapped to -1 if they are below boundaries[0] or mapped to ncolors if they are above boundaries[-1]. These are then converted to valid indices by Colormap.__call__.

  • extend ({'neither', 'both', 'min', 'max'}, default: 'neither') – Extend the number of bins to include one or both of the regions beyond the boundaries. For example, if extend is ‘min’, then the color to which the region between the first pair of boundaries is mapped will be distinct from the first color in the colormap, and by default a ~matplotlib.colorbar.Colorbar will be drawn with the triangle extension on the left or lower end.

Return type

int16 scalar or array

Notes

boundaries defines the edges of bins, and data falling within a bin is mapped to the color with the same index.

If the number of bins, including any extensions, is less than ncolors, the color index is chosen by linear interpolation, mapping the [0, nbins - 1] range onto the [0, ncolors - 1] range.

class psy_simple.colors.FixedColorMap(name, segmentdata, N=256, gamma=1.0)[source]

Bases: matplotlib.colors.LinearSegmentedColormap

Bug fixing colormap with same functionality as matplotlibs colormap

This class fixes a bug in the cartopy.mpl.geoaxes.GeoAxes.streamplot() method in python 3.4

Notes

To reproduce the error type in python 3.4:

>>> import cartopy.crs as ccrs
>>> import matplotlib.pyplot as plt
>>> import psyplot.project as psy
>>> maps = psy.plot.mapvector(
...     'test-t2m-u-v.nc', name=[['u', 'v']], plot='stream',
...     lonlatbox='Europe', color='absolute')
>>> plotter = maps[0].plotter
>>> x, y, u, v = plotter.plot._get_data()
>>> maps.close(True, True)
>>> ax = plt.axes(projection=ccrs.PlateCarree())
>>> ax.set_extent(plotter.lonlatbox.lonlatbox, crs=ccrs.PlateCarree())
>>> m = ax.streamplot(x, y, u, v, density=[1.0, 1.0],
...                   color=plotter.plot._kwargs['color'],
...                   norm=plotter.plot._kwargs['norm'])

This raises in matplotlib.colors, line 557, in matplotlib.colors.Colormap.__call__():

``xa = np.array([X])``
ValueError: setting an array element with a sequence.

Create colormap from linear mapping segments

segmentdata argument is a dictionary with a red, green and blue entries. Each entry should be a list of x, y0, y1 tuples, forming rows in a table. Entries for alpha are optional.

Example: suppose you want red to increase from 0 to 1 over the bottom half, green to do the same over the middle half, and blue over the top half. Then you would use:

cdict = {'red':   [(0.0,  0.0, 0.0),
                   (0.5,  1.0, 1.0),
                   (1.0,  1.0, 1.0)],

         'green': [(0.0,  0.0, 0.0),
                   (0.25, 0.0, 0.0),
                   (0.75, 1.0, 1.0),
                   (1.0,  1.0, 1.0)],

         'blue':  [(0.0,  0.0, 0.0),
                   (0.5,  0.0, 0.0),
                   (1.0,  1.0, 1.0)]}

Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1. For any input value z falling between x[i] and x[i+1], the output value of a given color will be linearly interpolated between y1[i] and y0[i+1]:

row i:   x  y0  y1
               /
              /
row i+1: x  y0  y1

Hence y0 in the first row and y1 in the last row are never used.

See also

LinearSegmentedColormap.from_list

Static method; factory function for generating a smoothly-varying LinearSegmentedColormap.

Methods:

from_list(*args, **kwargs)

Create a LinearSegmentedColormap from a list of colors.

static from_list(*args, **kwargs)[source]

Create a LinearSegmentedColormap from a list of colors.

Parameters
  • name (str) – The name of the colormap.

  • colors (array-like of colors or array-like of (value, color)) – If only colors are given, they are equidistantly mapped from the range \([0, 1]\); i.e. 0 maps to colors[0] and 1 maps to colors[-1]. If (value, color) pairs are given, the mapping is from value to color. This can be used to divide the range unevenly.

  • N (int) – The number of rgb quantization levels.

  • gamma (float) –

psy_simple.colors.get_cmap(name, lut=None)[source]

Returns the specified colormap.

Parameters
  • name (str or matplotlib.colors.Colormap) –

    If a colormap, it returned unchanged.

    Strings may be any valid colormap name suitable for the matplotlib.cm.get_cmap() function or one of the color lists defined in the ‘colors.cmaps’ key of the psyplot.rcParams dictionary (including their reversed color maps given via the ‘_r’ extension).

  • lut (int) – An integer giving the number of entries desired in the lookup table

Returns

The colormap specified by name

Return type

matplotlib.colors.Colormap

See also

show_colormaps

A function to display all available colormaps

Notes

Different from the :func::matpltolib.pyplot.get_cmap function, this function changes the number of colors if name is a matplotlib.colors.Colormap instance to match the given lut.

psy_simple.colors.show_colormaps(names=[], N=10, show=True, use_qt=None)[source]

Function to show standard colormaps from pyplot

Parameters
  • *args (str or matplotlib.colors.Colormap) –

    If a colormap, it returned unchanged.

    Strings may be any valid colormap name suitable for the matplotlib.cm.get_cmap() function or one of the color lists defined in the ‘colors.cmaps’ key of the psyplot.rcParams dictionary (including their reversed color maps given via the ‘_r’ extension).

  • N (int, optional) – Default: 11. The number of increments in the colormap.

  • show (bool, optional) – Default: True. If True, show the created figure at the end with pyplot.show(block=False)

  • use_qt (bool) – If True, use the psy_simple.widgets.color.ColormapDialog.show_colormaps, if False use a matplotlib implementation based on 1. If None, use the Qt implementation if it is running in the psyplot GUI.

Returns

Depending on use_qt, either an instance of the psy_simple.widgets.color.ColormapDialog or the matplotlib.figure.Figure

Return type

psy_simple.widgets.color.ColormapDialog or matplitlib.figure.Figure

References

1

http://matplotlib.org/1.2.1/examples/pylab_examples/show_colormaps.html