0

I want to change the fontsize of the lable on the y axis of a horizontal barplot (i.e. make the fontsize of "Question 1", "Question 2" bigger). I could not find the solution from the documentation of barh. Is it possible to do it. And if yes, where can the answer be found?

import matplotlib.pyplot as plt
import numpy as np
 
x = np.array(["Question 1", "Question 2", "Question 3", "Question 4"])
y = np.array([3, 8, 1, 10])
 
plt.barh(x, y)
plt.tight_layout()
plt.show()

enter image description here

ecjb
  • 5,169
  • 12
  • 43
  • 79
  • Does this answer your question? [How to change the font size on a matplotlib plot](https://stackoverflow.com/questions/3899980/how-to-change-the-font-size-on-a-matplotlib-plot) – Thrasy Jan 10 '22 at 09:57
  • While this reference is helpful, I wonder if it possible to change the font size for y labels independently from x labels. – hilberts_drinking_problem Jan 10 '22 at 09:59
  • Does this answer your question? [Python: How to increase/reduce the fontsize of x and y tick labels?](https://stackoverflow.com/questions/34001751/python-how-to-increase-reduce-the-fontsize-of-x-and-y-tick-labels) – Jody Klymak Jan 10 '22 at 10:41

1 Answers1

2

Use plt.yticks:

import matplotlib.pyplot as plt
import numpy as np
 
x = np.array(["Question 1", "Question 2", "Question 3", "Question 4"])
y = np.array([3, 8, 1, 10])
 
plt.barh(x, y)
plt.yticks(fontsize=20)
plt.tight_layout()
plt.show()

enter image description here

Corralien
  • 109,409
  • 8
  • 28
  • 52
  • Many thanks for your answer @Corralien. Could you tell me where I could have found it? – ecjb Jan 10 '22 at 10:11
  • 1
    This is the starting point: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html. All functions to use with `plt`. – Corralien Jan 10 '22 at 10:15
  • Many thanks for your comment @Corralien. Now when looking at the information of `yticks()` function there: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.yticks.html, it does not seem to take `fontsize` as argument. How then does it? – ecjb Jan 10 '22 at 10:23
  • 1
    Check `**kwargs` arguments, you have a link to [`Text`](https://matplotlib.org/stable/api/text_api.html#matplotlib.text.Text) :) I know it's a real maze :) – Corralien Jan 10 '22 at 10:26