-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolobar.py
More file actions
90 lines (81 loc) · 2.78 KB
/
Copy pathcolobar.py
File metadata and controls
90 lines (81 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
Make a colorbar as a separate figure.
'''
from matplotlib import pyplot, mpl
import numpy as np
def exp_colormap(cmap=None, horizontal=True, titlestr='$|v|$',
cmin=0.0, cmax=1.0, figratio=20, exp2tikz=True):
"""
Parameters:
-----------
cmap : `colormap`, optional
as in the `matplotlib.mpl.cm` module, defaults to `coolwarm`
cmin : real, optional
minimal value of the variable described, defaults to 0
cmax : real, optional
maximal value of the variable described, defaults to 1
titlestr : string, optional
title for the color map
horizontal : boolean, optional
whether to return a horizontal colorbar, defaults to `False`
figratio : real, optional
ratio of longer edge to shorter edge
"""
reso = 200
figs = (figratio, 1)
dataextend = [cmin, cmax, 0, 0.05*(cmax-cmin)]
xrg = np.linspace(cmin, cmax, reso)
yrg = np.array([[1], [1]])
zrg = yrg*xrg
if cmap is None:
cmap = mpl.cm.jet_r
if horizontal:
zrg = zrg.T
figs = (1, figratio)
dataextend = [0, 0.05*(cmax-cmin), cmin, cmax]
# Make a figure and axes with dimensions as desired.
fig = pyplot.figure(figsize=figs)
ax1 = fig.add_subplot(111, aspect='equal')
if horizontal:
ax1.set_xticklabels([])
ax1.set_xticks([])
else:
ax1.set_yticklabels([])
ax1.set_yticks([])
ax1.set_title(titlestr)
pyplot.imshow(zrg, cmap=cmap, extent=dataextend)
if exp2tikz:
from matplotlib2tikz import save as tikz_save
fname = 'colorbar.tikz'
extraaxiopts = frozenset([
'scaled x ticks=false,\n' +
'xtick={,,},\n' +
'xticklabels={,,},\n' +
# 'xlabel={$\phantom{x_0}$,\n' + # for align with domainaxis
'extra y ticks={\n' +
' \\pgfkeysvalueof{/pgfplots/ymin},\n' +
' \\pgfkeysvalueof{/pgfplots/ymax}\n' +
'},\n' +
'every extra y tick/.style={\n' +
' font=\\large,\n' +
' tick style=transparent, \n' +
' yticklabel pos=right,\n' +
' /pgf/number format/.cd,\n' +
' fixed,\n' +
' precision=3\n' +
'},'])
tikz_save(fname,
figureheight='\\figureheight',
figurewidth='\\figurewidth',
extra=extraaxiopts,
)
print 'Plot saved to ' + fname
print 'You may want to add \n'
print 'scaled x ticks=false,'
print 'xtick={,,},'
print 'xticklabels={,,},'
# print 'yticklabels={{{0},,{1}}}, \n'.format(cmin, cmax)
print 'to the axis definition'
pyplot.show(block=False)
if __name__ == '__main__':
exp_colormap()