In [1]:
import matplotlib
from matplotlib.pyplot import get_cmap, colorbar, legend
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
In [2]:
def create_figure(params):
dpi = 96.
width = int(params['width'])
height = int(params['height'])
fig = plt.figure(frameon=False, dpi=dpi, figsize=(width, height), facecolor=None, edgecolor=None)
fig.set_alpha(0)
fig.set_figwidth(width / dpi)
fig.set_figheight(height / dpi)
return fig
In [12]:
p = dict(cmin=30,
cmax=300,
width=25,
height=500,
colormap='jet',
showlabel=True,
units='degrees',
showvalues=True,
logscale=False)
fig = create_figure(p)
ax = fig.add_axes([0.05, 0.05, 1.0, 1.0])
if p['logscale'] is True:
CNorm = matplotlib.colors.LogNorm(vmin=p['cmin'], vmax=p['cmax'], clip=False)
else:
CNorm = matplotlib.colors.Normalize(vmin=p['cmin'], vmax=p['cmax'], clip=False)
cb = matplotlib.colorbar.ColorbarBase(ax, cmap=get_cmap(p['colormap']), norm=CNorm, orientation='vertical')
if p['showvalues'] is False:
cb.set_ticks([])
else:
if p['logscale']:
ticks = np.linspace(p['cmin'], p['cmax'], 5)
cb.set_ticks(ticks)
cb.set_ticklabels(ticks)
if p['showlabel']:
cb.set_label(p['units'])
In [13]:
p = dict(cmin=30,
cmax=300,
width=500,
height=25,
colormap='jet',
showlabel=True,
units='degrees',
showvalues=True,
logscale=False)
fig = create_figure(p)
ax = fig.add_axes([0.05, 0.05, 1.0, 1.0])
if p['logscale'] is True:
CNorm = matplotlib.colors.LogNorm(vmin=p['cmin'], vmax=p['cmax'], clip=False)
else:
CNorm = matplotlib.colors.Normalize(vmin=p['cmin'], vmax=p['cmax'], clip=False)
cb = matplotlib.colorbar.ColorbarBase(ax, cmap=get_cmap(p['colormap']), norm=CNorm, orientation='horizontal')
if p['showvalues'] is False:
cb.set_ticks([])
else:
if p['logscale']:
ticks = np.linspace(p['cmin'], p['cmax'], 5)
cb.set_ticks(ticks)
cb.set_ticklabels(ticks)
if p['showlabel']:
cb.set_label(p['units'])
In [66]:
p = dict(cmin=30,
cmax=300,
width=50,
height=500,
colormap='jet',
showlabel=True,
units='degrees',
showvalues=True,
logscale=False,
numcontours=10)
fig = create_figure(p)
ax = fig.add_axes([0.05, 0.05, 1.0, 1.0])
if p['logscale'] is True:
CNorm = matplotlib.colors.LogNorm(vmin=p['cmin'], vmax=p['cmax'], clip=False)
else:
CNorm = matplotlib.colors.Normalize(vmin=p['cmin'], vmax=p['cmax'], clip=False)
levs = np.linspace(p['cmin'], p['cmax'], p['numcontours'])
x, y = np.meshgrid(np.ones(2), np.ones(2))
cs = ax.contourf(x, y, x, levels=levs, norm=CNorm, cmap=get_cmap(p['colormap']), extend='both')
cb = colorbar(mappable=cs, cax=ax, orientation='vertical', spacing='proportional', extendrect=False)
if p['showvalues'] is False:
cb.set_ticks([])
else:
cb.set_ticks(levs)
cb.set_ticklabels(levs)
if p['showlabel']:
cb.set_label(p['units'])
In [67]:
p = dict(cmin=30,
cmax=300,
width=50,
height=500,
colormap='jet',
showlabel=True,
units='degrees',
showvalues=True,
logscale=True,
numcontours=10)
fig = create_figure(p)
ax = fig.add_axes([0.05, 0.05, 1.0, 1.0])
if p['logscale'] is True:
CNorm = matplotlib.colors.LogNorm(vmin=p['cmin'], vmax=p['cmax'], clip=False)
else:
CNorm = matplotlib.colors.Normalize(vmin=p['cmin'], vmax=p['cmax'], clip=False)
levs = np.hstack(([p['cmin']-3], np.linspace(p['cmin'], p['cmax'], p['numcontours']), [p['cmax']+40]))
x, y = np.meshgrid(np.ones(2), np.ones(2))
cs = ax.contourf(x, y, x, levels=levs, norm=CNorm, cmap=get_cmap(p['colormap']))
cb = colorbar(mappable=cs, cax=ax, orientation='vertical', spacing='proportional', extendrect=False)
if p['showvalues'] is False:
cb.set_ticks([])
else:
cb.set_ticks(levs[1:-1])
cb.set_ticklabels(levs[1:-1])
if p['showlabel']:
cb.set_label(p['units'])
In [70]:
p = dict(cmin=30,
cmax=300,
width=500,
height=50,
colormap='jet',
showlabel=True,
units='degrees',
showvalues=True,
logscale=False,
numcontours=10)
fig = create_figure(p)
ax = fig.add_axes([0.05, 0.05, 1.0, 1.0])
if p['logscale'] is True:
CNorm = matplotlib.colors.LogNorm(vmin=p['cmin'], vmax=p['cmax'], clip=False)
else:
CNorm = matplotlib.colors.Normalize(vmin=p['cmin'], vmax=p['cmax'], clip=False)
levs = np.linspace(p['cmin'], p['cmax'], p['numcontours'])
x, y = np.meshgrid(np.ones(2), np.ones(2))
cs = ax.contourf(x, y, x, levels=levs, norm=CNorm, cmap=get_cmap(p['colormap']), extend='both')
cb = colorbar(mappable=cs, values=levs, cax=ax, orientation='horizontal', spacing='proportional', extendrect=False)
if p['showvalues'] is False:
cb.set_ticks([])
else:
cb.set_ticks(levs)
cb.set_ticklabels(levs)
if p['showlabel']:
cb.set_label(p['units'])
In [73]:
p = dict(cmin=30,
cmax=300,
width=500,
height=50,
colormap='jet',
showlabel=True,
units='degrees',
showvalues=True,
logscale=True,
numcontours=10)
fig = create_figure(p)
ax = fig.add_axes([0.05, 0.05, 1.0, 1.0])
if p['logscale'] is True:
CNorm = matplotlib.colors.LogNorm(vmin=p['cmin'], vmax=p['cmax'], clip=False)
else:
CNorm = matplotlib.colors.Normalize(vmin=p['cmin'], vmax=p['cmax'], clip=False)
levs = np.hstack(([p['cmin']-3], np.linspace(p['cmin'], p['cmax'], 10), [p['cmax']+40]))
x, y = np.meshgrid(np.ones(2), np.ones(2))
cs = ax.contourf(x, y, x, levels=levs, norm=CNorm, cmap=get_cmap(p['colormap']))
cb = colorbar(mappable=cs, cax=ax, orientation='horizontal', spacing='proportional', extendrect=True)
if p['showvalues'] is False:
cb.set_ticks([])
else:
cb.set_ticks(levs[1:-1])
cb.set_ticklabels(levs[1:-1])
if p['showlabel']:
cb.set_label(p['units'])
In [75]:
p = dict(cmin=30,
cmax=300,
width=50,
height=50,
colormap='jet',
showlabel=True,
units='degrees',
showvalues=True,
logscale=False,
numcontours=10)
fig = create_figure(p)
ax = fig.add_axes([0.05, 0.05, 1.0, 1.0])
ax.set_axis_off()
if p['logscale'] is True:
CNorm = matplotlib.colors.LogNorm(vmin=p['cmin'], vmax=p['cmax'], clip=False)
else:
CNorm = matplotlib.colors.Normalize(vmin=p['cmin'], vmax=p['cmax'], clip=False)
levs = np.linspace(p['cmin'], p['cmax'], p['numcontours'])
if p['showvalues'] is True:
levs_labels = [str(x) for x in levs ]
levs_labels[0] = "< " + str(p['cmin'])
levs_labels[-1] = "> " + str(p['cmax'])
else:
levs_labels = [ '' for x in range(levs.size) ]
x, y = np.meshgrid(np.arange(2), np.arange(2))
cs = ax.contourf(x, y, x, levels=levs, norm=CNorm, cmap=get_cmap(p['colormap']), extend='max')
proxy = [plt.Rectangle((0, 0), 0, 0, fc=pc.get_facecolor()[0]) for pc in cs.collections]
cb = legend(proxy, levs_labels, frameon=False)
if p['showlabel']:
cb.set_title(p['units'])
In [11]:
p = dict(cmin=30,
cmax=300,
width=50,
height=50,
colormap='jet',
showlabel=True,
units='degrees',
showvalues=True,
logscale=False,
numcontours=10)
fig = create_figure(p)
ax = fig.add_axes([0.05, 0.05, 1.0, 1.0])
ax.set_axis_off()
if p['logscale'] is True:
CNorm = matplotlib.colors.LogNorm(vmin=p['cmin'], vmax=p['cmax'], clip=False)
else:
CNorm = matplotlib.colors.Normalize(vmin=p['cmin'], vmax=p['cmax'], clip=False)
levs = np.linspace(p['cmin'], p['cmax'], p['numcontours'])
levs_labels = levs
if p['showvalues'] is False:
levs_labels = [ '' for x in range(levs.size) ]
x, y = np.meshgrid(np.ones(2), np.ones(2))
cs = ax.contourf(x, y, x, levels=levs, norm=CNorm, cmap=get_cmap(p['colormap']), extend='max')
proxy = [plt.Rectangle((0, 0), 0, 0, fc=pc.get_facecolor()[0]) for pc in cs.collections]
cb = legend(proxy, levs_labels, frameon=False)
if p['showlabel']:
cb.set_title(p['units'])
In [ ]: