Skip to content

Instantly share code, notes, and snippets.

@jtratner
Created May 2, 2012 01:34
Show Gist options
  • Save jtratner/2572911 to your computer and use it in GitHub Desktop.
Save jtratner/2572911 to your computer and use it in GitHub Desktop.
DefaultColor(defaultdict)
matplotcolors= ['b','g','r','c','m','y','k']
defaultcolorlist = ["#00af64","#0b61a4","#ff9200","#ff4900","#9c02a7"]
defaultcolorlist.extend(matplotcolors)
from collections import defaultdict
class DefaultColor(defaultdict):
""" defaultdict that returns sequential elements of a list(if not specified, a color) each time it is
called Note that if input list is len(1) it will always return the same
value"""
def __init__(self,colorlst = defaultcolorlist):
self.color_len = len(colorlst)
self.current_index = 0
self.colors = colorlst
super(DefaultColor, self).__init__(lambda : self.sendcolor())
def sendcolor(self):
# check if we're at end of color list, if so, reset index
color_index = (self.current_index
if self.current_index < self.color_len
else 0)
# increment current_index for next time
self.current_index = color_index + 1
# return the color
return self.colors[color_index]
@jtratner
Copy link
Author

jtratner commented May 2, 2012

This works, the only issue is finding a few more colors. (makes it easier to graph when you don't have to deal with color choices initially). Need a defaultdict here because want to be consistent with colors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment