2016-02-17 101 views
3

我想爲x軸添加一個標籤,併爲y軸添加一個標籤。一個用於Seaborn FacetGrid子圖的共享x軸標籤(佈局/間距?)

此外,顏色條標題多一點的彩色條空間的提示將不勝感激。

我標誌着可以使用幫助與# <---- Help Please!

# this chunk seems to be necessary for plotting in my virtualenv. 
import matplotlib 
matplotlib.use('TkAgg') 
% matplotlib inline 

import matplotlib.pyplot as plt 
import pandas as pd 
import numpy as np 
import itertools 
import seaborn as sns 

import platform 

print("python version {}".format(platform.python_version())) 
# python version 3.5.1 
print("seaborn version {}".format(sns.__version__)) 
# seaborn version 0.7.0 

methods=['method 1', 'method2', 'method 3', 'method 4'] 
times = range(0, 100, 10) 
data = pd.DataFrame(list(
     itertools.product(methods, times, times))) 
data.columns = ['method','x var', 'y var'] 
data['x var'] = data['x var']*10 
data['score'] = np.random.sample(data.shape[0]) 
print(data.head()) 

def facet_heatmap(data, color, **kws): 
    data = data.pivot(index='y var', columns='x var', 
         values='score') 
    # Pass kwargs to heatmap 
    sns.heatmap(data, cmap='summer', **kws) 

with sns.plotting_context(font_scale=5.5): 
    g = sns.FacetGrid(data, col="method", col_wrap=2, 
         size=3, aspect=1) 

# Create a colorbar axes 
cbar_ax = g.fig.add_axes([.96, .3, .02, .4], 
         title='could use \n more space') # <---- Help Please! 

# Specify the colorbar axes and limits 
g = g.map_dataframe(facet_heatmap, 
        cbar_ax=cbar_ax, 
        vmin=0, vmax=1) 

# add a supertitle, you bet. 
plt.subplots_adjust(top=0.85) 
supertitle = "This is a supertitle, you bet." 
g.fig.suptitle(supertitle, size=18) 

# rotate x labels 
g.set_xticklabels(rotation=90) 

g.set_titles(col_template="{col_name}", 
      fontweight='bold', fontsize=18) 
g.fig.subplots_adjust(right=.9) # Add space so the colorbar doesn't overlap the plot 

# ---- add one label for x axis and one for y-axis ----- 

g.fig.text(0.4, 0.1, s='way too high!',fontdict={'fontsize':16}) # <---- Help Please! 
plt.figtext(0.4,0.02,"this looks bad",fontdict={'fontsize':16}) # <---- Help Please! 
# add y-axis label too  [enter image description here][1]# <---- Help Please! 

注意的地方:我建立對this post頂部,並增加了一些好用的東西,如supertitle,旋轉的X標籤,彩條的標題。

+3

您必須使用'g.fig.text'。 – mwaskom

+0

感謝您確認,@ mwaskom。我將學習如何添加文本,而不會與其他情節圖形重疊。 – jmatsen

+2

答案與前一個基本相同:您需要使用'g.fig.subplots_adjust',但現在在左側和底部添加空格。 – mwaskom

回答

0

根據@mwaskom的評論,您必須使用g.fig.text()來添加標籤並g.fig.subplots_adjust()在左側和底部添加空間以避免重疊。