diff --git a/cluster_stats.py b/cluster_stats.py index 4a19ec2..0e5a099 100644 --- a/cluster_stats.py +++ b/cluster_stats.py @@ -135,26 +135,46 @@ def stackplot(ax, x_signal, y_signals, legend_location='best'): ax.stackplot(x_signal, list(y_signals.values())) plt.legend(list(y_signals.keys())) else: - # emulating missing Axes.stackplot method - colors = get_rgb_palette(num_colors=len(y_signals), saturation=1.0, value=0.8) # ['blue', 'orange', 'green', 'purple', 'yellow', 'cyan'] # we sort the signals according to their labels to group departments of the same lab together on the plot ordered_y_signals = [] ordered_labels = [] + labs = set() + owner_to_lab = {} for label, y_signal in sorted(y_signals.items()): ordered_y_signals.append(y_signal) ordered_labels.append(label) + lab = label.split('.')[0] + labs.add(lab) + owner_to_lab[label] = lab + + # instead of evenly spreading the colour in the spectrum, we try to separate groups of colors (1 group for a lab), to ease reading the graph + num_unused_colors_between_labs = 1 + num_unused_colors = len(labs) * num_unused_colors_between_labs + num_owners = len(y_signals) + num_colors = num_owners + num_unused_colors + # emulating missing Axes.stackplot method + colors = get_rgb_palette(num_colors=num_colors, saturation=1.0, value=0.8) # ['blue', 'orange', 'green', 'purple', 'yellow', 'cyan'] + y = np.row_stack(ordered_y_signals) # this call to 'cumsum' (cumulative sum), passing in your y data, # is necessary to avoid having to manually order the datasets y_stack = np.cumsum(y, axis=0) # a 3x10 array + last_lab = None + color_index = 0 for series_index in range(len(y_signals)): if series_index == 0: from_signal = 0 else: from_signal = y_stack[series_index - 1, :] - ax.fill_between(x_signal, from_signal, y_stack[series_index, :], color=colors[series_index], lw=0.0) - p = plt.Rectangle((0, 0), 0, 0, color=colors[series_index]) + owner = ordered_labels[series_index] + lab = owner_to_lab[owner] + color_index = (color_index + 1) % num_colors + if lab != last_lab: + color_index = (color_index + num_unused_colors_between_labs) % num_colors + last_lab = lab + ax.fill_between(x_signal, from_signal, y_stack[series_index, :], color=colors[color_index], lw=0.0) + p = plt.Rectangle((0, 0), 0, 0, color=colors[color_index]) ax.add_patch(p) if legend_location == 'outside right':