Skip to content

Feature request: hierarchical axes labels on bar charts #2088

Closed
@gjx

Description

@gjx

Related to matplotlib/matplotlib#1257.

This is a feature available in Excel and OpenOffice 3.3.

Example:
http://i.imgur.com/8SjMs.png

I think there may be a simple way to do it with matplotlib's table to construct the hierarchical axes labels. I'm not an expert in matplotlib or pandas, but I've cobbled together a small demo:

import itertools

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

row_labels = itertools.product(['ASub', 'DSub'], ['LD', 'LS'], ['GD', 'GS'],
                               ['Attn', 'Dist'])
midx = pd.MultiIndex.from_tuples([row for row in row_labels],
                                 names=['Group', 'Local', 'Global', 'Attn'])
df = pd.DataFrame({'mean': np.random.rand(16)}, index=midx)

ax = df.plot(kind='bar')
ax.xaxis.set_visible(False)

cell_text = np.array(df.index.tolist()).T
ax.table(cellText=cell_text, rowLabels=df.index.names, cellLoc = 'center')

plt.subplots_adjust(bottom=0.2)
plt.show()

Produces the plot
http://i.imgur.com/OB8wi.png

There's probably a smarter way to populate the table in order to place the labels at the center of the merged cells as well as fix the spacing clashes.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions