matplotlib 更改子图的“xticks”和“Yticks”的字体大小和粗体[重复]

ygya80vv  于 2023-11-22  发布在  其他
关注(0)|答案(2)|浏览(159)

此问题在此处已有答案

How to change the font size on a matplotlib plot(16个回答)
两年前关闭。
简单地说,我用Python中的matplotlib库绘制了这个直方图,其中有三个子图。我的问题是:如何将“xticks”和“Yticks”的字体大小都增加到24,并使其加粗。

代码

  1. y12 = data_all_2[0]
  2. y22= data_all_2[1]
  3. y32= data_all_2[2]
  4. bins =[0, 50, 100,150, 200,250]
  5. names = ['legend_1', 'legend_2', 'legend_3', 'legend_4']
  6. colors = ['b','c','r', 'g']
  7. fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, sharex=True)
  8. p1 = ax0.hist([y32[0:20], y32[20:34], y32[34:56], y32[56:68]], bins, histtype='bar', stacked=True,
  9. label=names, rwidth=0.4, color = colors,edgecolor='black')
  10. ax0.legend(loc=0, fontsize='x-large',prop={'size':8},)
  11. p2 = ax1.hist([y12[0:20], y12[20:34], y12[34:56], y12[56:68]], bins, histtype='bar', stacked=True,
  12. label=names, rwidth=0.4, color = colors,edgecolor='black')
  13. ax1.legend(loc=0, fontsize='x-large',prop={'size':8},)
  14. p3 = ax2.hist([y22[0:20], y22[20:34], y22[34:56], y22[56:68]], bins, histtype='bar', stacked=True,
  15. label=names, rwidth=0.4, color = colors,edgecolor='black')
  16. ax2.legend(loc=0, fontsize='x-large',prop={'size':8},)
  17. fig.subplots_adjust(hspace=0.6)
  18. fig.text(0.5, 0.03,'bins' ,fontsize=14, fontweight='bold', ha='center', va='center' )
  19. fig.text(0.05, 0.5, 'Y axis', fontsize=16, fontweight='bold', ha='center',
  20. va='center',rotation='vertical')
  21. # Add the rectangular patch to the Axes
  22. xmin, xmax = 113,188
  23. trans = transforms.blended_transform_factory(ax0.transData, fig.transFigure)
  24. r = patches.Rectangle(xy=(xmin,0.1), width=xmax-xmin, height=0.750, transform=trans, fc='none',
  25. ec='m', lw=3, linestyle='dashed')
  26. fig.add_artist(r)
  27. plt.show()

字符串

输出


的数据

xxls0lw8

xxls0lw81#

答案是:

  1. for a in [ax0, ax1, ax2]:
  2. for label in (a.get_xticklabels() + a.get_yticklabels()):
  3. label.set_fontsize(24)
  4. label.set_fontweight('bold').

字符串

r8xiu3jd

r8xiu3jd2#

好吧,正如我上面评论的那样,人们已经可以在this question中找到解决方案,所以我仍然认为这个问题应该作为重复关闭。
无论如何,试试这个例子:

  1. font_props = {'family' : 'normal',
  2. 'weight' : 'bold',
  3. 'size' : 24}
  4. for a in [ax0,ax1,ax2]:
  5. for label in (a.get_xticklabels() + a.get_yticklabels()):
  6. label.set_font(font_props)

字符串

相关问题