Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

pycama / src / matplotlib_colormaps.py @ 816:d52976b6ccfc

History | View | Annotate | Download (2.72 KB)

1
#!/usr/bin/env python3
2

    
3
# PyCAMA Copyright (c) 2016, KNMI (Maarten Sneep).
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining a
6
# copy of this software and associated documentation files (the "Software"),
7
# to deal in the Software without restriction, including without limitation
8
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
# and/or sell copies of the Software, and to permit persons to whom the
10
# Software is furnished to do so, subject to the following conditions:
11
#
12
# - The above copyright notice and this permission notice shall be included
13
#   in all copies or substantial portions of the Software.
14
#
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
# OTHER DEALINGS IN THE SOFTWARE.
22

    
23
import argparse
24
import textwrap
25

    
26
import numpy as np
27
import matplotlib.pyplot as plt
28

    
29
import temis_color_tables
30

    
31
# based on an example from matplotlib.org:
32
# http://matplotlib.org/1.2.1/examples/pylab_examples/show_colormaps.html
33

    
34
def get_list():
35
    # Get a list of the colormaps in matplotlib.  Ignore the ones that end with
36
    # '_r' because these are simply reversed versions of ones that don't end
37
    # with '_r'
38
    maps = [m for m in plt.colormaps() if not m.endswith("_r")]
39
    maps.sort(key=lambda s: s.lower())
40
    return maps
41

    
42
def display(maps):
43
    nmaps = len(maps) + 1
44
    a = np.linspace(0, 1, 256).reshape(1,-1)
45
    a = np.vstack((a,a))
46

    
47
    fig = plt.figure(figsize=(6,12))
48
    fig.subplots_adjust(top=0.99, bottom=0.01, left=0.2, right=0.99)
49
    for i,m in enumerate(maps):
50
        ax = plt.subplot(nmaps, 1, i+1)
51
        plt.axis("off")
52
        plt.imshow(a, aspect='auto', cmap=plt.get_cmap(m), origin='lower')
53
        pos = list(ax.get_position().bounds)
54
        fig.text(pos[0] - 0.01, pos[1], m, fontsize=10, horizontalalignment='right')
55

    
56
    plt.show()
57

    
58
def write(maps):
59
    print("Available color maps:")
60
    wrapper = textwrap.TextWrapper(initial_indent="    ")
61
    print("\n    ".join(wrapper.wrap(", ".join(maps) + '.')))
62

    
63
if __name__ == "__main__":
64
    parser = argparse.ArgumentParser(description='Print the available color map names')
65
    parser.add_argument('-s', '--show', dest='cmd',
66
                        action="store_const",
67
                        default=write,
68
                        const=display,
69
                        help="Show the colormaps")
70
    args = parser.parse_args()
71

    
72
    args.cmd(get_list())