mgkit.plots.heatmap module

New in version 0.1.14.

Code related to heatmaps.

mgkit.plots.heatmap.baseheatmap(data, ax, norm=None, cmap=None, xticks=None, yticks=None, fontsize=18, meshopts=None, annot=False, annotopts=None)[source]

Changed in version 0.2.3: added annot and annot_args arguments

A basic heatmap using matplotlib.pyplot.pcolormesh(). It expect a pandas.DataFrame.

Note

Rows a plot bottom to up, while the columns left to right. Change the order of the DataFrame if needed.

Parameters
  • data (pandas.DataFrame) – matrix to plot. The DataFrame labels are used

  • ax – axes to use

  • norm – if needed, matplotlib.colors.BoundaryNorm or matplotlib.colors.Normalize can be used to fine tune the colors

  • cmap (None, matplotlib.colors.ListedColormap) – color map to use

  • xticks (None, dict) – dictionary with additional options to pass to set_xticklabels

  • yticks (None, dict) – dictionary with additional options to pass to set_yticklabels

  • fontsize (int) – font size to use for the labels

  • meshopts (None, dict) – additional options to pass to matplotlib.pyplot.pcolormesh()

  • annot (bool) – if True the values of the matrix will be added

  • annot_args (None, dict) – dictionary with the options for the annotations. The option format is a function that returns the formatted number, defaults to a number with no decimal part

Returns

the return value of matplotlib.pyplot.pcolormesh()

Return type

matplotlib.collections.QuadMesh

mgkit.plots.heatmap.dendrogram(data, ax, method='complete', orientation='top', use_dist=True, dist_func=<function pdist>)[source]

Changed in version 0.1.16: added use_dist and dist_func parameters

Plots a dendrogram of the clustered rows of the given matrix; if the columns are to be clustered, the transposed matrix needs to be passed.

Parameters
  • data (pandas.DataFrame) – matrix to plot. The DataFrame labels are used

  • ax – axes to use

  • method (str) – clustering method used, internally scipy.cluster.hierarchy.linkage() is used.

  • orientation (str) – direction for the plot. top, bottom, left and right are accepted; top will draw the leaves at the bottom.

  • use_dist (bool) – if True, the function dist_func will be applied to data to get a distance matrix

  • dist_func (func) – distance function to be used

Returns

The dendrogram plotted, as returned by scipy.cluster.hierarchy.dendrogram()

mgkit.plots.heatmap.grouped_spine(groups, labels, ax, which='y', spine='right', spine_opts=None, start=0)[source]

Changed in version 0.2.0: added va, ha keys to spine_opts, changed the label positioning

Changed in version 0.2.5: added start parameter

Changes the spine of an heatmap axis given the groups of labels.

Note

It should work for any plot, but was not tested

Parameters
  • groups (iterable) – a nested list where each is element is a list containing the labels that belong to that group.

  • labels (iterable) – an iterable with the labels of the groups. Needs to be in the same order as groups

  • ax – axis to use (same as heatmap)

  • which (str) – to specify the axis, either x or y

  • spine (str) – position of the spine. if which is x accepted values are top and bottom, if which is y left and right are accepted

  • spine_opts (dict) – additional options to pass to the spine class

  • start (int) – the start coordinate for the grouped spine. Defaults to 0

mgkit.plots.heatmap.heatmap_clustered(data, figsize=10, 5, cmap=None, norm=None)[source]

Plots a heatmap clustered on both rows and columns.

Parameters
  • data (pandas.DataFrame) – matrix to plot. The DataFrame labels are used

  • figsize (tuple) – passed to mgkit.plots.utils.get_grid_figure()

  • cmap (None, matplotlib.colors.ListedColormap) – color map to use

  • norm – if needed, matplotlib.colors.BoundaryNorm or matplotlib.colors.Normalize can be used to fine tune the colors