Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the matplotlib axes tick labels fontweight to bold?

I was trying the following code:

fig, ax1 = plt.subplots()
#data is a geopandas DataFrame 
data.plot(ax=ax1, kind='bar', color= barcolor, width= 0.8)
ax1.set_yticklabels(ax1.get_yticklabels(), weight='bold')
ax1.set_xticklabels(ax1.get_xticklabels(), rotation=0, weight='bold', size=12)

Only x-axis gets affected by this code and y-axis labels are getting removed.

like image 430
Subrat Prasad Avatar asked Oct 13 '26 12:10

Subrat Prasad


2 Answers

Change get_yticklabels() to get_yticks works for me:

fig, ax1 = plt.subplots()
ax1.bar([0,1],[2,3])
ax1.set_yticklabels(ax1.get_yticks(), weight='bold')
ax1.set_xticklabels(ax1.get_xticks(), rotation=0, weight='bold', size=12)

Output:

enter image description here

like image 186
Quang Hoang Avatar answered Oct 15 '26 03:10

Quang Hoang


Here is the answer for reasons behind this kind of error. It's because matplotlib avoids static positioning. Adding the following code just after data.plot() helped too:

fig.canvas.draw()
like image 27
Subrat Prasad Avatar answered Oct 15 '26 02:10

Subrat Prasad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!