Skip to content
Advertisement

Matplotlib stacked histogram label

Here is my picture. I need to make label for those bars however every upper layer contains lower layer – so the label should containt grouped colors, i.e. blue – dataset 1, blue/orange – dataset 2, blue/orange/green – dataset 3 and finally blue/orange/green/purple – dataset 4. Is it plausible to make it? Thank you.

enter image description here

JavaScript

Advertisement

Answer

The following approach uses the tuple legend handler (HandlerTuple) to combine the legend handles. It produces a horizontal layout, while maybe a vertical stacking would be more interesting.

The code starts with creating some test data, supposing C is an Nx4 array of integers. The bin edges are set at halves to make sure that floating point accuracy wouldn’t place values in the wrong bin.

JavaScript

matplotlib legend using HandlerTuple

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement