Skip to content

Instantly share code, notes, and snippets.

@oneross
Last active December 28, 2015 13:29
Show Gist options
  • Select an option

  • Save oneross/7508515 to your computer and use it in GitHub Desktop.

Select an option

Save oneross/7508515 to your computer and use it in GitHub Desktop.

Revisions

  1. oneross revised this gist Apr 11, 2014. No changes.
  2. oneross revised this gist Apr 11, 2014. No changes.
  3. oneross revised this gist Nov 17, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions heatmap
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,14 @@ import matplotlib.pyplot as plt

    def heatmap(df,
    edgecolors='w',
    cmap=mpl.cm.RdBu,
    cmap=mpl.cm.gist_stern,
    log=False):
    width = len(df.columns)/4
    height = len(df.index)/4

    fig, ax = plt.subplots(figsize=(width,height))

    heatmap = ax.pcolor(df,
    heatmap = ax.pcolor(df.fillna(0), # useful for mapping missing values, which pop with gist_stern
    edgecolors=edgecolors, # put white lines between squares in heatmap
    cmap=cmap,
    norm=mpl.colors.LogNorm() if log else None)
  4. @gjx gjx revised this gist Oct 28, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions heatmap
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,8 @@ def heatmap(df,
    edgecolors='w',
    cmap=mpl.cm.RdBu,
    log=False):
    width = len(df.columns)/6
    height = len(df.index)/6
    width = len(df.columns)/4
    height = len(df.index)/4

    fig, ax = plt.subplots(figsize=(width,height))

  5. @gjx gjx revised this gist Oct 28, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions heatmap
    Original file line number Diff line number Diff line change
    @@ -36,15 +36,17 @@ def heatmap(df,
    import matplotlib as mpl
    import matplotlib.pyplot as plt

    def binary_heatmap(df):
    def binary_heatmap(df):
    df = dataframe[::-1] # reverse df to put first row at top (last row at origin)

    width = len(df.columns)/5
    height = len(df.index)/5

    fig, ax = plt.subplots(figsize=(width,height))

    heatmap = ax.pcolor(df,
    edgecolors='k', # put black lines between squares in heatmap
    cmap=mpl.cm.binary)
    cmap=mpl.cm.binary) # black/white colomarp

    ax.autoscale(tight=True) # get rid of whitespace in margins of heatmap
    ax.set_aspect('equal') # ensure heatmap cells are square
  6. @gjx gjx revised this gist Oct 3, 2013. 1 changed file with 27 additions and 3 deletions.
    30 changes: 27 additions & 3 deletions heatmap
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,11 @@
    """Plots a Pandas dataframe as a heatmap"""
    import matplotlib as mpl
    import matplotlib.pyplot as plt

    def heatmap(df,
    edgecolors='w',
    cmap=mpl.cm.RdBu,
    log=False):
    """Plots a Pandas dataframe as a heatmap"""

    log=False):
    width = len(df.columns)/6
    height = len(df.index)/6

    @@ -30,5 +29,30 @@ def heatmap(df,
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", "3%", pad="1%")
    plt.colorbar(heatmap, cax=cax)


    """Binary Heatmap"""

    import matplotlib as mpl
    import matplotlib.pyplot as plt

    def binary_heatmap(df):
    width = len(df.columns)/5
    height = len(df.index)/5

    fig, ax = plt.subplots(figsize=(width,height))

    heatmap = ax.pcolor(df,
    edgecolors='k', # put black lines between squares in heatmap
    cmap=mpl.cm.binary)

    ax.autoscale(tight=True) # get rid of whitespace in margins of heatmap
    ax.set_aspect('equal') # ensure heatmap cells are square
    ax.xaxis.set_ticks_position('top') # put column labels at the top
    ax.tick_params(bottom='off', top='off', left='off', right='off') # turn off ticks

    plt.yticks(np.arange(len(df.index)) + 0.5, df.index)
    plt.xticks(np.arange(len(df.columns)) + 0.5, df.columns, rotation=90)


    plt.tight_layout()
  7. @gjx gjx created this gist Sep 22, 2013.
    34 changes: 34 additions & 0 deletions heatmap
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import matplotlib as mpl
    import matplotlib.pyplot as plt

    def heatmap(df,
    edgecolors='w',
    cmap=mpl.cm.RdBu,
    log=False):
    """Plots a Pandas dataframe as a heatmap"""

    width = len(df.columns)/6
    height = len(df.index)/6

    fig, ax = plt.subplots(figsize=(width,height))

    heatmap = ax.pcolor(df,
    edgecolors=edgecolors, # put white lines between squares in heatmap
    cmap=cmap,
    norm=mpl.colors.LogNorm() if log else None)

    ax.autoscale(tight=True) # get rid of whitespace in margins of heatmap
    ax.set_aspect('equal') # ensure heatmap cells are square
    ax.xaxis.set_ticks_position('top') # put column labels at the top
    ax.tick_params(bottom='off', top='off', left='off', right='off') # turn off ticks

    plt.yticks(np.arange(len(df.index)) + 0.5, df.index)
    plt.xticks(np.arange(len(df.columns)) + 0.5, df.columns, rotation=90)

    # ugliness from http://matplotlib.org/users/tight_layout_guide.html
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", "3%", pad="1%")
    plt.colorbar(heatmap, cax=cax)

    plt.tight_layout()