|
3 | 3 | import pandas as pd
|
4 | 4 | import matplotlib as mpl
|
5 | 5 | import matplotlib.pyplot as plt
|
| 6 | +from matplotlib import gridspec |
6 | 7 | from itertools import product
|
7 | 8 |
|
8 | 9 | import seaborn
|
9 | 10 | from inspect import signature
|
10 | 11 | from seaborn.axisgrid import FacetGrid, JointGrid, PairGrid, Grid
|
| 12 | +import seaborn.matrix as sm |
| 13 | +from seaborn.matrix import ClusterGrid |
11 | 14 | from seaborn._core import VectorPlotter, variable_type, categorical_order
|
12 | 15 | from seaborn import utils
|
13 | 16 | from seaborn.utils import _check_argument, adjust_legend_subtitles, _draw_figure
|
@@ -461,3 +464,114 @@ def __setattr_for_clustergrid__(self, key, value):
|
461 | 464 | else:
|
462 | 465 | super.__setattr__(self, key, value)
|
463 | 466 |
|
| 467 | +def __init_for_clustergrid__(self, data, pivot_kws=None, z_score=None, standard_scale=None, |
| 468 | + figsize=None, row_colors=None, col_colors=None, mask=None, |
| 469 | + dendrogram_ratio=None, colors_ratio=None, cbar_pos=None): |
| 470 | + """Grid object for organizing clustered heatmap input on to axes""" |
| 471 | + try: |
| 472 | + import scipy |
| 473 | + except: |
| 474 | + raise RuntimeError("ClusterGrid requires scipy to be available") |
| 475 | + |
| 476 | + if isinstance(data, pd.DataFrame): |
| 477 | + self.data = data |
| 478 | + else: |
| 479 | + self.data = pd.DataFrame(data) |
| 480 | + |
| 481 | + self.data2d = self.format_data(self.data, pivot_kws, z_score, |
| 482 | + standard_scale) |
| 483 | + |
| 484 | + self.mask = sm._matrix_mask(self.data2d, mask) |
| 485 | + |
| 486 | + self._figure = Grid._figure #Modified by Hideto |
| 487 | + self._figsize = figsize |
| 488 | + self._figure.set_size_inches(figsize) |
| 489 | + #self._figure = plt.figure(figsize=figsize) |
| 490 | + |
| 491 | + self.row_colors, self.row_color_labels = \ |
| 492 | + self._preprocess_colors(data, row_colors, axis=0) |
| 493 | + self.col_colors, self.col_color_labels = \ |
| 494 | + self._preprocess_colors(data, col_colors, axis=1) |
| 495 | + |
| 496 | + try: |
| 497 | + row_dendrogram_ratio, col_dendrogram_ratio = dendrogram_ratio |
| 498 | + except TypeError: |
| 499 | + row_dendrogram_ratio = col_dendrogram_ratio = dendrogram_ratio |
| 500 | + |
| 501 | + try: |
| 502 | + row_colors_ratio, col_colors_ratio = colors_ratio |
| 503 | + except TypeError: |
| 504 | + row_colors_ratio = col_colors_ratio = colors_ratio |
| 505 | + |
| 506 | + width_ratios = self.dim_ratios(self.row_colors, |
| 507 | + row_dendrogram_ratio, |
| 508 | + row_colors_ratio) |
| 509 | + height_ratios = self.dim_ratios(self.col_colors, |
| 510 | + col_dendrogram_ratio, |
| 511 | + col_colors_ratio) |
| 512 | + |
| 513 | + nrows = 2 if self.col_colors is None else 3 |
| 514 | + ncols = 2 if self.row_colors is None else 3 |
| 515 | + |
| 516 | + self.gs = gridspec.GridSpec(nrows, ncols, |
| 517 | + width_ratios=width_ratios, |
| 518 | + height_ratios=height_ratios, wspace=0, hspace=0) |
| 519 | + |
| 520 | + self.ax_row_dendrogram = self._figure.add_subplot(self.gs[-1, 0]) |
| 521 | + self.ax_col_dendrogram = self._figure.add_subplot(self.gs[0, -1]) |
| 522 | + self.ax_row_dendrogram.set_axis_off() |
| 523 | + self.ax_col_dendrogram.set_axis_off() |
| 524 | + |
| 525 | + self.ax_row_colors = None |
| 526 | + self.ax_col_colors = None |
| 527 | + |
| 528 | + if self.row_colors is not None: |
| 529 | + self.ax_row_colors = self._figure.add_subplot( |
| 530 | + self.gs[-1, 1]) |
| 531 | + if self.col_colors is not None: |
| 532 | + self.ax_col_colors = self._figure.add_subplot( |
| 533 | + self.gs[1, -1]) |
| 534 | + |
| 535 | + self.ax_heatmap = self._figure.add_subplot(self.gs[-1, -1]) |
| 536 | + if cbar_pos is None: |
| 537 | + self.ax_cbar = self.cax = None |
| 538 | + else: |
| 539 | + # Initialize the colorbar axes in the gridspec so that tight_layout |
| 540 | + # works. We will move it where it belongs later. This is a hack. |
| 541 | + self.ax_cbar = self._figure.add_subplot(self.gs[0, 0]) |
| 542 | + self.cax = self.ax_cbar # Backwards compatibility |
| 543 | + self.cbar_pos = cbar_pos |
| 544 | + |
| 545 | + self.dendrogram_row = None |
| 546 | + self.dendrogram_col = None |
| 547 | + |
| 548 | +def __plot_for_clustergrid__(self, metric, method, colorbar_kws, row_cluster, col_cluster, row_linkage, col_linkage, tree_kws, **kws): |
| 549 | + # heatmap square=True sets the aspect ratio on the axes, but that is |
| 550 | + # not compatible with the multi-axes layout of clustergrid |
| 551 | + if kws.get("square", False): |
| 552 | + msg = "``square=True`` ignored in clustermap" |
| 553 | + warnings.warn(msg) |
| 554 | + kws.pop("square") |
| 555 | + |
| 556 | + colorbar_kws = {} if colorbar_kws is None else colorbar_kws |
| 557 | + |
| 558 | + self.plot_dendrograms(row_cluster, col_cluster, metric, method, |
| 559 | + row_linkage=row_linkage, col_linkage=col_linkage, |
| 560 | + tree_kws=tree_kws) |
| 561 | + |
| 562 | + try: |
| 563 | + xind = self.dendrogram_col.reordered_ind |
| 564 | + except AttributeError: |
| 565 | + xind = np.arange(self.data2d.shape[1]) |
| 566 | + |
| 567 | + try: |
| 568 | + yind = self.dendrogram_row.reordered_ind |
| 569 | + except AttributeError: |
| 570 | + yind = np.arange(self.data2d.shape[0]) |
| 571 | + |
| 572 | + self.plot_colors(xind, yind, **kws) |
| 573 | + self.plot_matrix(colorbar_kws, xind, yind, **kws) |
| 574 | + self._figure.set_size_inches((1,1)) |
| 575 | + #mpl.rcParams["figure.subplot.hspace"] = hspace |
| 576 | + #mpl.rcParams["figure.subplot.wspace"] = wspace |
| 577 | + return self |
0 commit comments