我正在为我的数据创建一个混淆矩阵图。在图的旁边,我要放置一个颜色栏,并想要更改颜色栏刻度标签的字体大小。我在互联网上搜索了一段时间,但由于我正在使用imshow
创建颜色条,因此无法弄清楚如何更改颜色条的刻度的字体大小。这可能是因为以这种方式创建颜色栏不是在网络上大多数位置(例如here和here)完成/建议的通常方式。因此,我需要您的帮助。这是我创建绘图并在其旁边添加颜色栏的方法:
data=np.array([[0.83, 0.6, 0.76],[0.59, 0.46, 0.52],[0.62, 0.58, 0.88]])
xTicksMajor, yTicksMajor = [0.5, 1.5, 2.5], [0.5, 1.5, 2.5]
xTicksMinor, yTicksMinor = [0, 1, 2], [0, 1, 2]
fig, ax = plt.subplots()
cmapProp = {'drawedges': True, 'boundaries': np.linspace(0, 1, 13, endpoint=True).round(2)}
m = ax.imshow(data, cmap=plt.cm.get_cmap('Oranges'))
m.set_clim(0, 1)
ax.figure.colorbar(m, ax=ax, **cmapProp)
ax.set_xticks(xTicksMajor)
ax.set_yticks(yTicksMajor)
ax.set_xticks(xTicksMinor, minor=True)
ax.set_yticks(yTicksMinor, minor=True)
ax.yaxis.grid(True, color='black', linestyle='-', linewidth=0.5)
ax.xaxis.grid(True, color='black', linestyle='-', linewidth=0.5)
thresh = data.max() / 1.4
for i, j in itertools.product(range(data.shape[0]), range(data.shape[1])):
ax.text(j, i, format(data[i, j], '.2f'),
horizontalalignment="center",
verticalalignment='center',
color="black" if data[i, j] > thresh else "dimgrey",
fontsize=26)
fig.savefig('temp.png', dpi=200)
plt.close()
我尝试如下更改刻度的字体大小:
cmapProp = {'drawedges': True, 'boundaries': np.linspace(0, 1, 13, endpoint=True).round(2), 'fontsize': 14}
但是这给了我以下错误:
TypeError:init()获得了意外的关键字参数'fontsize'
我想知道如何更改颜色栏旁边的刻度标签的字体大小?随意提出建议,例如以其他方式创建颜色条,以便轻松更改字体大小。
此外,以上代码在图中显示的结果如下:
怎么样:
...
fig, ax = plt.subplots()
cmapProp = {'drawedges': True, 'boundaries': np.linspace(0, 1, 13, endpoint=True).round(2)}
m = ax.imshow(data, cmap=plt.cm.get_cmap('Oranges'))
m.set_clim(0, 1)
# And here try this:
cbar = ax.figure.colorbar(m, ax=ax, **cmapProp)
cbar.ax.tick_params(labelsize=25) # set your label size here
...