2016-06-29 62 views
1

我有以下代碼:傳說與除seafile熱圖標籤

from string import letters 
import numpy as np 
import pandas as pd 
import seaborn as sns 
import matplotlib.pyplot as plt 

sns.set(style="white") 

# Compute the correlation matrix 
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD')) 
corr = df.corr() 

# Generate a mask for the upper triangle 
mask = np.zeros_like(corr, dtype=np.bool) 
mask[np.triu_indices_from(mask)] = True 

# Set up the matplotlib figure 
f, ax = plt.subplots(figsize=(11, 9)) 

# Generate a custom diverging colormap 
cmap = sns.diverging_palette(220, 10, as_cmap=True) 

# Draw the heatmap with the mask and correct aspect ratio 
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, 
      square=True, #xticklabels=5, #yticklabels=5, 
      linewidths=.5, cbar_kws={"shrink": .5}, ax=ax) 
plt.show() 

怎麼可能給一個額外的傳說的地方,情節的,說覺得像正確的:

A: This is my first label 
B: ... 
C: ... 
D: ... 

我還想爲關聯欄標註簡單的「相關性」。這些數據當然不是真實的數據。

回答

1

關於你的第一個問題,這裏是我能夠做到的:

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

sns.set(style="white") 

cols = list('ABCD') 
# Compute the correlation matrix 
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=cols) 
corr = df.corr() 

# Generate a mask for the upper triangle 
mask = np.zeros_like(corr, dtype=np.bool) 
mask[np.triu_indices_from(mask)] = True 

# Set up the matplotlib figure 
f, ax = plt.subplots(figsize=(11, 9)) 

# Generate a custom diverging colormap 
cmap = sns.diverging_palette(220, 10, as_cmap=True) 

# Draw the heatmap with the mask and correct aspect ratio 
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, 
      square=True, #xticklabels=5, #yticklabels=5, 
      linewidths=.5, cbar_kws={"shrink": .5}, ax=ax) 
pseudo_lines = [] 
strs = ['first', 'second', 'third', 'fourth'] 
for c, s in zip(cols, strs): 
    line = mlines.Line2D([], [], color='blue', 
     marker=r"${}:.This.is.my.{}.label$".format(c, s), 
     markersize=170, linestyle = 'None') 
    pseudo_lines.append(line) 
plt.legend(handles=pseudo_lines, labelspacing=2) 

plt.show() 
+0

謝謝你,我很欣賞你的幫助。我會將這個問題留待一段時間,也許有人也有相關欄的解決方案。再次感謝你! – Ohumeronen