Skip to content

Instantly share code, notes, and snippets.

@AdrianLsk
Created May 25, 2018 19:58
Show Gist options
  • Save AdrianLsk/0959e3004f0dc10dadf7201ce8e4cc69 to your computer and use it in GitHub Desktop.
Save AdrianLsk/0959e3004f0dc10dadf7201ce8e4cc69 to your computer and use it in GitHub Desktop.
Matplotlib utils
"""Inspired by:
https://gist.github.com/jakevdp/91077b0cae40f8f8244a
http://joseph-long.com/writing/colorbars/
"""
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
def discrete_cmap(N, base_cmap=None):
"""Create an N-bin discrete colormap from the specified input map"""
base = plt.cm.get_cmap(base_cmap)
color_list = base(np.linspace(0, 1, N))
cmap_name = base.name + str(N)
return base.from_list(cmap_name, color_list, N)
def colorbar(mappable, ticks):
ax = mappable.axes
fig = ax.figure
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
return fig.colorbar(mappable, cax=cax, ticks=ticks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment