lbl-anp / becquerel

Becquerel is a Python package for analyzing nuclear spectroscopic measurements.
Other
43 stars 16 forks source link

Fitter.custom_plot fails if more than three model components #257

Closed markbandstra closed 3 years ago

markbandstra commented 3 years ago

Fitter.custom_plot fails with a IndexError: list index out of range if there are more than three components to the model. The culprit is this line:

        colors = ["#1f78b4", "#33a02c", "#6a3d9a"]

A possible fix would be to extend the list like this:

        colors = ["C0", "C2", "C4", "C5", "C6", "C7", "C8", "C9"]

where I left out "C1" and "C3" since they are similar to other colors used in the plot (the centroids and best fit, respectively).

jvavrek commented 3 years ago

I'm in favour of this fix. No more magic hex values! Could we automate the colour selection instead of a fixed list of 8? An fstring like f"C{i}" could handle it if we correctly tell it to skip C1 and C3 and those colours every time they come around in the cycle.

markbandstra commented 3 years ago

@jvavrek Good point, why keep the list a fixed but longer length if we can make it infinite.

How about this? Probably could be simplified further, and there are certainly other ways to do this:

import matplotlib
colors = [matplotlib.colors.to_rgb(c) for c in ["C0", "C2", "C4", "C5", "C6", "C7", "C8", "C9"]]

plt.figure()
plt.gca().set_prop_cycle(color=colors)
jvavrek commented 3 years ago

@markbandstra nice, I didn't know about set_prop_cycle. Something like that would be great.