Skip to content

ENH: plotting function for "2D histograms"/"dynamic spectra" (similar to heatmap) #33560

Open
@johan12345

Description

@johan12345

Is your feature request related to a problem?

I often need to plot a heatmap of a DataFrame which uses an IntervalIndex as its columns (and, usually, time as its index). Such a plot could also be called a "dynamic spectrum" or "2D histogram" and is used to quickly get an idea of how a spectrum develops over time.

This is slightly different from what is usually considered as a heatmap (see #19008 for an example) as the bins are not necessarily equidistant and there is not necessarily a separate label for each bin. The y axis (which is used for the IntervalIndex) could even have logarithmic scaling.

Describe the solution you'd like

This could use the same API df.plot(type='heatmap') as suggested in #19008 and switch between appropriate axis scaling/labeling modes depending on whether a CategoricalIndex, IntervalIndex or other types of indices are used.

Describe alternatives you've considered

My current implementation (see below) uses matplotlib's pcolormesh, but needs to do some fiddling with the bin edges to work correctly.

Matplotlib's hist2d does not work for this use case, because the data is already stored in histogrammed form - the histogram and its bins don't need to be calculated, just plotted.

Seaborn's heatmap function seems to be limited to plotting categorical data, so both IntervalIndex and DatetimeIndex are displayed as categorical data with one label per bin, equidistant spacing, and values on the y axis sorted from top to bottom instead of bottom to top:

Additional context

My current implementation looks similar to this:

binedges = np.append(df.columns.left, df.columns.right[-1])
X, Y = np.meshgrid(df.index, binedges)
pcm = ax.pcolormesh(X, Y, df.values.T)

# then add labels, colorbar etc.

This only works if the IntervalIndex has no gaps and is non-overlapping, which would have to be checked first.

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