Search code examples
matplotlibhierarchical-clusteringseaborndendrogram

Anaconda Python 3.4 using Seaborn figure too tight


I am developing hierarchal clusters in the form of dendrograms using Python 3.4 and Seaborn, using the work of Olga Botvinnik (http://nbviewer.ipython.org/gist/olgabot/bfe1e3638af3eea52fb1#). My goal is to cluster U.S. cities based on greenhouse gas emissions. I was able to successfully read my csv file and create a figure with residential and commercial buildings emissions on the x axis and city names on the y axis, but I cannot see any of the city names because they are too squished together. The image needs to be elongated so that I can read it. Can anyone point me in a good direction?

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.read_csv('/Users/JCMartel 1/Desktop/ghg_directory/rescom.csv', index_col=0)
data.index = data.index.map(lambda x: x.strip())

sns.clustermap(data);

#Need to improve layout
fig = plt.gcf()
fig.savefig('clustermap_bbox_tight.png', bbox_inches='tight')

Solution

  • Following is the final script that I am using:

    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    data = pd.read_csv('/Users/JCMartel 1/Desktop/ghg_directory/ghgmodel4.csv', index_col=0)
    data.index = data.index.map(lambda x: x.strip())
    
    cmap = sns.cubehelix_palette(as_cmap=True, rot=-.3, light=1)
    sns.clustermap(data, col_cluster=False, cmap=cmap, linewidths=.5, figsize=(8, 30))