2017-09-15 2 views
3

Ich habe in Seaborn eine Heatmap erstellt, und ich habe eine entsprechende horizontale Farbleiste. Ich habe der Farbleiste einen Titel hinzugefügt, der Titel erscheint jedoch unterhalb der Farbleiste, wenn ich ihn oben haben möchte. Gibt es einige, warum ich das ändern kann? Und ist es möglich, die Schriftgröße des Titels und die Größe der Beschriftungen in der Farbleiste zu ändern?Beweglicher Titel über der Farbleiste in Seaborn Heatmap

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 

So sieht es bisher aus. Ich möchte den Titel "Anzahl der Unfälle" über der Farbleiste, aber unter der Heatmap. und ich möchte, dass die Farbleiste so breit wie die Heatmap ist.

enter image description here

Antwort

2

Plot Ihre colorbar separat in wie in diesem Beispiel zeichnen. Für weitere Informationen lesen Sie die Kommentare:

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

Ich sehe, was Sie hier getan haben, aber ich will nicht die „Anzahl der Verkehrsunfälle pro Tag und Stunde Kombination“ title, dass man sich bewegen soll über der Heatmap sein. Was ich will, ist, dass der Titel "Anzahl der Unfälle" über der Farbleiste liegt, aka in der gleichen Position, in der die "Anzahl der Verkehrsunfälle pro Tag & Stunde Kombination" im Bild oben ist. –

+0

machen Titel für Achsen 'Axt' nicht 'cbar_ax'. – Serenity

+0

Aber das ist wieder Platz 1 - genau wie ich es anfangs hatte. Ich möchte, dass der Titel "Anzahl der Unfälle" ÜBER der Farbleiste und die Ylabels (Wochentage) horizontal sind ... –