如何更改 Matplotlib 中颜色条刻度标签的字体大小?
How to change the font size of tick labels of a colorbar in Matplotlib?
我正在为我的数据创建混淆矩阵图。在绘图旁边,我放置了一个颜色条并想要更改颜色条刻度标签的字体大小。我在互联网上搜索了一段时间,但无法弄清楚如何更改颜色栏刻度的字体大小,因为我正在使用 imshow
创建颜色栏。这可能是因为在网络上的大多数地方(例如 here and here),以这种方式创建颜色条不是通常的方式 done/suggested。所以我需要你的帮助。以下是我创建绘图并在其旁边添加颜色条的方式:
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() got an unexpected keyword argument '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
...
输出:
粗体 标签:
...
cbar = ax.figure.colorbar(m, ax=ax, **cmapProp)
cbar.ax.tick_params(labelsize=25)
for tick in cbar.ax.yaxis.get_major_ticks():
tick.label2.set_fontweight('bold')
...
输出:
我正在为我的数据创建混淆矩阵图。在绘图旁边,我放置了一个颜色条并想要更改颜色条刻度标签的字体大小。我在互联网上搜索了一段时间,但无法弄清楚如何更改颜色栏刻度的字体大小,因为我正在使用 imshow
创建颜色栏。这可能是因为在网络上的大多数地方(例如 here and here),以这种方式创建颜色条不是通常的方式 done/suggested。所以我需要你的帮助。以下是我创建绘图并在其旁边添加颜色条的方式:
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() got an unexpected keyword argument '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
...
输出:
粗体 标签:
...
cbar = ax.figure.colorbar(m, ax=ax, **cmapProp)
cbar.ax.tick_params(labelsize=25)
for tick in cbar.ax.yaxis.get_major_ticks():
tick.label2.set_fontweight('bold')
...
输出: