Skip to content
Advertisement

python matplotlib “merge” x axis labels for multi index

I would like to “merge” the first level of multi index, i.e. have a centered “USA” and “EU” instead of the tupples.

Minimal Exmaple

JavaScript

Output enter image description here

Desired output Using excel, one gets the desired output (almost) by default:

enter image description here

The ideal solution is simple, short and does not require any manual adjustments if more regions / sectrs are added.

Advertisement

Answer

There are other ways to make the x-axis ticks into two lines: the first line is taken from the multi-index and made into a list; the second line is placed from the multi-index list by the text function. Hiding the existing labels is handled by the plot parameter.

JavaScript

enter image description here

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