从matplotlib: colorbars and its text labels
借来的代码我正在研究类似的数字,但我无法弄清楚如何将“cbar.ax.text”的字体更改为Arial。
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 :(得分:0)
我链接的问题不是完全重复(oops),但是让您了解如何编辑全局字体属性。将以下行添加到代码顶部:
font = {'family' : 'sans-serif',
'sans-serif': 'Ariel'}
matplotlib.rc('font', **font)
请参阅matplotlib的documentation和此问题:How to change the font size on a matplotlib plot。