Skip to content

Convenience function: DataFrame to linked-brushing plot #206

@danielballan

Description

@danielballan

The following is adapts the linked brushing example for a pandas DataFrame, generating NxN axes for DataFrame with N columns. This seems like a common use case, and a useful shortcut for data exploration. Do we want to get into the business of providing convenience functions like this?

def linked_axes(df):
    "Draw an array of axes with linked brushing."
    L = df.shape[1]
    fig, ax = plt.subplots(L, L, sharex="col", sharey="row", figsize=(2*L, 2*L))
    fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95,
                        hspace=0.1, wspace=0.1)

    for i in range(L):
        for j in range(L):
            points = ax[L - 1 - i, j].scatter(df.iloc[:, j], df.iloc[:, i],
                                              c='k', s=40, alpha=0.2)
            if i == 0:
                ax[L - 1 - i, j].set_xlabel(df.columns[j])
            if j == 0:
                ax[L - 1 - i, j].set_ylabel(df.columns[i])

    # remove tick labels
    for axi in ax.flat:
        for axis in [axi.xaxis, axi.yaxis]:
            axis.set_major_formatter(plt.NullFormatter())

    # Here we connect the linked brush plugin
    plugins.connect(fig, plugins.LinkedBrush(points))
    return fig

We could quibble about how the axes labels and ticks are handled, but there won't be a good solution until tick formatting is fully supported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions