A Notebook for generating legends based off of a gradient color scheme.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
colormap_name = 'YlOrBr' # see http://matplotlib.org/xkcd/examples/color/colormaps_reference.html for list of maps
legend_label = 'Firmicutes Abundance'
max_label = '99%'
min_label = '0%'
fontsize=25
filename = legend_label.replace(' ', '_').replace('(', '').replace(')', '') + '.png'
gradient = np.linspace(0, 1, 256)[::-1]
gradient = np.vstack((gradient, gradient)).T
# adapted from http://worksofscience.net/matplotlib/twinx
fig = figure(figsize=(8,0.5)[::-1])
ax = gca()
axD = fig.add_subplot(ax)
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(colormap_name))
axD.set_ylabel('')
axD.set_xticks([], [])
axD.set_yticks([], [])
axD.set_ylabel(legend_label, fontsize=fontsize)
axD.set_xlabel(min_label, fontsize=fontsize)
axU = axD.twiny()
axU.set_ylabel('')
axU.set_xticks([], [])
axU.set_yticks([], [])
axU.set_xlabel(max_label, fontsize=fontsize)
savefig(filename, transparent=True, bbox_inches='tight')