2017-09-15 139 views
3

我在Seaborn中製作了一個熱圖,並且我有一個對應的水平顏色條。我已經爲顏色條添加了標題,但是,當我真正需要顏色條時,標題會出現在顏色條下方。有什麼我可以改變這一點嗎?是否可以更改標題的字體大小和彩條上標籤的大小?在Seaborn熱圖中移動顏色條上方的標題

fig, ax = plt.subplots(figsize=(30,12)) 
graph = sns.heatmap(df_pivot, cmap="Blues", linecolor="white", linewidths=0.1, 
      cbar_kws={"orientation": "horizontal", "shrink":0.40, "aspect":40, "label": "Number of accidents"}) 


ax.set_title("Number of traffic accidents per day & hour combination", 
fontsize=30, fontweight="bold") 

from matplotlib import rcParams 
rcParams['axes.titlepad'] = 70 # Space between the title and graph 

locs, labels = plt.yticks() # Rotating row labels 
plt.setp(labels, rotation=0) # Rotating row labels 

ax.xaxis.tick_top() # x axis on top 
ax.xaxis.set_label_position('top') 

graph.tick_params(axis='both',labelsize=15) # Tick label size 
graph 

這就是目前爲止的樣子。我希望「事故數量」標題高於顏色條,但低於熱圖。我希望顏色條與熱圖一樣寬。

enter image description here

回答

2

情節您在單獨繪製彩條就像這個例子。欲瞭解更多信息請閱讀註釋:

import matplotlib.pyplot as plt 
import numpy as np; np.random.seed(0) 
import seaborn as sns; sns.set() 

flights = sns.load_dataset("flights") 
flights = flights.pivot("month", "year", "passengers") 

# set height ratios of heatmap and coresponding colorbar 
# hspace shows space between plots 
# adjust hspace to put your title of colorbar correctly 
grid_kws = {"height_ratios": (.9, .05), "hspace": .15} 
f, (ax, cbar_ax) = plt.subplots(2, gridspec_kw=grid_kws, figsize=(30,12)) 
ax = sns.heatmap(flights, ax=ax, 
    cbar_ax=cbar_ax, 
    cbar_kws={"orientation": "horizontal","label": "Number of accidents"}) 

# set title and set font of title 
ax.set_title("Number of traffic accidents per day & hour combination", 
fontsize=30, fontweight="bold") 

# set font size of colorbar labels 
cbar_ax.tick_params(labelsize=25) 

plt.show() 

enter image description here

+0

我看到你在這裏做的,但我不想動了「每天每小時及組合的交通意外數字」的稱號,那一個應該在熱圖上方。我想要的是,「事故次數」標題位於彩條上方,也就是上圖中「每天和每小時組合的交通事故數量」的相同位置。 –

+0

爲軸'ax'而不是'cbar_ax'做標題。 – Serenity

+0

但是這又回到了第一個方面 - 我最初是如何做到的。我希望「事故數量」標題高於顏色條,以及ylabels(星期幾)水平。 –