1

Shortly, I have plotted this histogram figure with three subplots using the matplotlib library in python. My question is: How to increase the font size of both 'xticks' and 'Yticks' to 24 and make it Bold as well.

Code

y12 = data_all_2[0]
y22= data_all_2[1]
y32= data_all_2[2]
bins =[0, 50, 100,150, 200,250]
names = ['legend_1', 'legend_2', 'legend_3', 'legend_4']
colors = ['b','c','r', 'g']

fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, sharex=True)
p1 = ax0.hist([y32[0:20], y32[20:34], y32[34:56], y32[56:68]],  bins, histtype='bar',   stacked=True, 
label=names,  rwidth=0.4, color = colors,edgecolor='black')
ax0.legend(loc=0, fontsize='x-large',prop={'size':8},)

p2 = ax1.hist([y12[0:20], y12[20:34], y12[34:56], y12[56:68]],  bins, histtype='bar',   stacked=True, 
 label=names,  rwidth=0.4, color = colors,edgecolor='black')
ax1.legend(loc=0, fontsize='x-large',prop={'size':8},)

p3 = ax2.hist([y22[0:20], y22[20:34], y22[34:56], y22[56:68]],  bins, histtype='bar',   stacked=True, 
label=names,  rwidth=0.4, color = colors,edgecolor='black')
ax2.legend(loc=0, fontsize='x-large',prop={'size':8},)
fig.subplots_adjust(hspace=0.6)

fig.text(0.5, 0.03,'bins' ,fontsize=14, fontweight='bold', ha='center', va='center' )
fig.text(0.05, 0.5, 'Y axis', fontsize=16, fontweight='bold', ha='center', 
 va='center',rotation='vertical')
# Add the rectangular patch to the Axes
xmin, xmax = 113,188
trans = transforms.blended_transform_factory(ax0.transData, fig.transFigure)
r = patches.Rectangle(xy=(xmin,0.1), width=xmax-xmin, height=0.750, transform=trans, fc='none', 
ec='m', lw=3, linestyle='dashed')
fig.add_artist(r)

plt.show()

Output

enter image description here

Mohsen Ali
  • 655
  • 1
  • 9
  • 30

2 Answers2

2

The answer is:

for a in [ax0, ax1, ax2]:
for label in (a.get_xticklabels() + a.get_yticklabels()):
    label.set_fontsize(24)
    label.set_fontweight('bold').
Mohsen Ali
  • 655
  • 1
  • 9
  • 30
1

Well, as I commented above one can find the solution e.g. in this question already, so I still think this question should be closed as duplicate.

Anyhow, try this for example:

font_props = {'family' : 'normal',
              'weight' : 'bold',
              'size'   : 24}
        
for a in [ax0,ax1,ax2]:
    for label in (a.get_xticklabels() + a.get_yticklabels()):
        label.set_font(font_props)
Asmus
  • 5,117
  • 1
  • 16
  • 21
  • Your code gives this error regarding set_font (font_props), [AttributeError: 'Text' object has no attribute 'set_font']. Therefore, the correct is as follows: for a in [ax0, ax1, ax2]: for label in (a.get_xticklabels() + a.get_yticklabels()): label.set_fontsize(24) label.set_fontweight('bold'). This code works fine with me. No need to use font_props. – Mohsen Ali Nov 22 '20 at 10:58
  • @MohsenAli Not sure which `matplotlib.__version__` you're on, but Text objects _do_ have the `.set_font()` method [in the latest versions of matplotlib](https://matplotlib.org/api/text_api.html?highlight=set_font#matplotlib.text.Text.set_font) – Asmus Nov 22 '20 at 11:42