Skip to content

Commit

Permalink
Assign random plot colors for bands that don't have preassigned colors
Browse files Browse the repository at this point in the history
  • Loading branch information
kboone committed Sep 17, 2019
1 parent f89a77d commit 3ca8fe3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions avocado/astronomical_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pandas as pd
from scipy.optimize import minimize

from .instruments import band_central_wavelengths, band_plot_colors
from .instruments import band_central_wavelengths, get_band_plot_color
from .utils import logger

class AstronomicalObject():
Expand Down Expand Up @@ -368,7 +368,7 @@ def plot_light_curve(self, show_gp=True, verbose=False, axis=None,
for band_idx, band in enumerate(self.bands):
mask = observations['band'] == band
band_data = observations[mask]
color = band_plot_colors[band]
color = get_band_plot_color(band)

axis.errorbar(band_data['time'], band_data['flux'],
band_data['flux_error'], fmt='o', c=color,
Expand Down
28 changes: 28 additions & 0 deletions avocado/instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,31 @@
'lsstz': 'C3',
'lssty': 'goldenrod',
}

def get_band_plot_color(band):
"""Return the plot color for a given band.
If the band does not yet have a color assigned to it, then a random color
will be assigned (in a systematic way).
Parameters
----------
band : str
The name of the band to use.
"""
if band in band_plot_colors:
return band_plot_colors[band]

print("No plot color assigned for band %s, assigning a random one." % band)

# Systematic random colors. We use the hash of the band name.
# Note: hash() uses a random offset in python 3 so it isn't consistent
# between runs!
import hashlib
hasher = hashlib.md5()
hasher.update(band.encode('utf8'))
hex_color = '#%s' % hasher.hexdigest()[-6:]

band_plot_colors[band] = hex_color

return hex_color

0 comments on commit 3ca8fe3

Please sign in to comment.