2017-05-30 5 views
0

-Code entlehnt matplotlib: colorbars and its text labelsWie kann ich die Schriftart von "cbar.ax.text" in Arial ändern?

ich auf eine ähnliche Figur arbeite, aber ich kann nicht herausfinden, wie die Schrift von „cbar.ax.text“ Arial zu ändern.

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.colors import ListedColormap 

#discrete color scheme 
cMap = ListedColormap(['white', 'green', 'blue','red']) 

#data 
np.random.seed(42) 
data = np.random.rand(4, 4) 
fig, ax = plt.subplots() 
heatmap = ax.pcolor(data, cmap=cMap) 

#legend 
cbar = plt.colorbar(heatmap) 

cbar.ax.get_yaxis().set_ticks([]) 
for j, lab in enumerate(['$0$','$1$','$2$','$>3$']): 
    cbar.ax.text(.5, (2 * j + 1)/8.0, lab, ha='center', va='center') 
cbar.ax.get_yaxis().labelpad = 15 
cbar.ax.set_ylabel('# of contacts', rotation=270) 


# put the major ticks at the middle of each cell 
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False) 
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False) 
ax.invert_yaxis() 

#lebels 
column_labels = list('ABCD') 
row_labels = list('WXYZ') 
ax.set_xticklabels(column_labels, minor=False) 
ax.set_yticklabels(row_labels, minor=False) 

plt.show() 
+0

Mögliches Duplikat von [So ändern Sie die Schriftgröße auf einem Matplotlib-Plot] (https://stackoverflow.com/questions/3899980/how-to-change-the-font-size-on-a-matplotlib-plot) – Luce

+0

@Luce Ich habe versucht, aber es ändert die Schriftart von allem außer den Etiketten der cbar. – user8087767

Antwort

0

Die Frage, die ich verbunden war nicht eine exakte Kopie (oops), sondern gibt Ihnen eine Vorstellung davon, wie die globalen Schrifteigenschaften zu bearbeiten. Fügen Sie die folgenden Zeilen an die Spitze der Sie den Code ein:

font = {'family' : 'sans-serif', 
     'sans-serif': 'Ariel'} 

matplotlib.rc('font', **font) 

die documentation von matplotlib Sehen und diese Frage: How to change the font size on a matplotlib plot.

+0

Es ändert nicht die Schriftart von der Cbar zu Arial. Es ändert die Achse, aber nicht die Farbleiste. – user8087767

+0

Wenn Sie das $ um die Textbeschriftungen entfernen, funktioniert es. I.e. change: 'für j, lab in enumerate (['$ 0 $', '$ 1 $', '$ 2 $', '$> 3 $']):' to 'für j, lab in enumerate (['0', '1', '2', '> 3']): ' – Luce

Verwandte Themen