- Change Font Size of elements in a Matplotlib plot
- How to change the font size in matplotlib?
- Examples
- 1. Change the global font size
- 2. Change font size of the tick labels
- 3. Change font size of the axes labels
- 4. Change font size of the axes title
- Как изменить размер шрифта на графике Matplotlib
- Пример 1: изменение размера шрифта всех элементов
- Пример 2: изменение размера шрифта заголовка
- Пример 3: изменение размера шрифта меток осей
- Пример 4: изменение размера шрифта галочек
- Бонус: восстановление размеров шрифта по умолчанию
- How to Change Font Sizes on a Matplotlib Plot
- Example 1: Change the Font Size of All Elements
- Example 2: Change the Font Size of the Title
- Example 3: Change the Font Size of the Axes Labels
- Example 4: Change the Font Size of the Tick Labels
- Bonus: Restore the Default Font Sizes
Change Font Size of elements in a Matplotlib plot
In this tutorial we’ll look at how to change the font size of text elements in a matplotlib plot.
How to change the font size in matplotlib?
You can change the global font size in matplotlib using rcparams. You can also change the font size of individual components such as tick labels, axes labels, axes titles, etc. The following is the syntax:
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
Introductory ⭐
Intermediate ⭐⭐⭐
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
import matplotlib.pyplot as plt plt.rcParams.update()
The above syntax changes the overall font size in matplotlib plots to 20. Note that, the default font size is 10. You can also change the font size of specific components (see the examples below)
Examples
Let’s look at some of the use cases of changing font size in matplotlib. First, we’ll create a simple matplotlib line plot and see the default font size.
import matplotlib.pyplot as plt plt.rcdefaults() # number of employees of A emp_count = [3, 20, 50, 200, 350, 400] year = [2014, 2015, 2016, 2017, 2018, 2019] # plot a line chart plt.plot(year, emp_count, 'o-g') # set axis titles plt.xlabel("Year") plt.ylabel("Employees") # set chart title plt.title("Employee Growth at A") plt.show()
The above code creates a line plot with text using the default font size.
1. Change the global font size
Let’s change the overall font size of the above plot. Since by default it is 10, we will increase that to 15 to see how the plot appears with a higher font size.
# update the overall font size plt.rcParams.update() # plot a line chart plt.plot(year, emp_count, 'o-g') # set axis titles plt.xlabel("Year") plt.ylabel("Employees") # set chart title plt.title("Employee Growth at A") plt.show()
You can see that the overall font size of the line chart is now considerably bigger. The tick labels, axis titles, plot title all are comparatively bigger than in the default plot.
2. Change font size of the tick labels
You can also set the sizes of individual elements in a matplotlib plot instead of changing the font size of the entire plot. To change the font size of only the tick labels for both the axes:
# reset the plot configurations to default plt.rcdefaults() # change the fontsize of the xtick and ytick labels plt.rc('xtick', labelsize=15) plt.rc('ytick', labelsize=15) # plot a line chart plt.plot(year, emp_count, 'o-g') # set axis titles plt.xlabel("Year") plt.ylabel("Employees") # set chart title plt.title("Employee Growth at A") plt.show()
Note that first, we’re using the rcdefaults() function to reset the matplotlib configurations to their defaults, and then we’re customizing our plot. You can see that the labels on the x-axis (2014, 2015, …) and the y-axis (0, 100, …) have a comparatively larger font size.
3. Change font size of the axes labels
Similarly, you can also change the size of the axes labels specifically without changing other font sizes.
# reset the plot configurations to default plt.rcdefaults() # change the fontsize of axes lables plt.rc('axes', labelsize=15) # plot a line chart plt.plot(year, emp_count, 'o-g') # set axis titles plt.xlabel("Year") plt.ylabel("Employees") # set chart title plt.title("Employee Growth at A") plt.show()
You can see that the axis labels “Employees” and “Year” have a larger font size compared to other text components of the plot.
4. Change font size of the axes title
You can also change the size of the axes title specifically without changing other font sizes.
# reset the plot configurations to default plt.rcdefaults() # change the fontsize of axes title plt.rc('axes', titlesize=20) # plot a line chart plt.plot(year, emp_count, 'o-g') # set axis titles plt.xlabel("Year") plt.ylabel("Employees") # set chart title plt.title("Employee Growth at A") plt.show()
You can see that the axes title is quite large compared to the axes labels and the tick labels.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having matplotlib version 3.2.2
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Как изменить размер шрифта на графике Matplotlib
Часто вам может понадобиться изменить размеры шрифта различных элементов на графике Matplotlib. К счастью, это легко сделать с помощью следующего кода:
import matplotlib.pyplot as plt plt.rc('font', size=10) #controls default text size plt.rc('axes', titlesize=10) #fontsize of the title plt.rc('axes', labelsize=10) #fontsize of the x and y labels plt.rc('xtick', labelsize=10) #fontsize of the x tick labels plt.rc('ytick', labelsize=10) #fontsize of the y tick labels plt.rc('legend', fontsize=10) #fontsize of the legend
В следующих примерах показано, как изменить размеры шрифта различных элементов на следующей диаграмме рассеяния matplotlib:
import matplotlib.pyplot as plt x = [3, 4, 6, 7, 8] y = [12, 14, 15, 19, 24] plt.scatter (x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Примечание.* Размер шрифта по умолчанию для всех элементов — 10* .
Пример 1: изменение размера шрифта всех элементов
Следующий код показывает, как изменить размер шрифта каждого элемента на графике:
#set font of all elements to size 15 plt.rc('font', size= 15 ) #create plot plt.scatter (x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Пример 2: изменение размера шрифта заголовка
Следующий код показывает, как изменить размер шрифта заголовка графика:
#set title font to size 50 plt.rc('axes', titlesize= 50 ) #create plot plt.scatter (x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Пример 3: изменение размера шрифта меток осей
Следующий код показывает, как изменить размер шрифта меток осей графика:
#set axes labels font to size 20 plt.rc('axes', labelsize= 20 ) #create plot plt.scatter (x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Пример 4: изменение размера шрифта галочек
В следующем коде показано, как изменить размер шрифта галочек на графике:
#set tick labels font to size 20 plt.rc('xtick', labelsize= 20 ) plt.rc('ytick', labelsize= 20 ) #create plot plt.scatter (x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Бонус: восстановление размеров шрифта по умолчанию
Вы можете использовать следующий код для восстановления размера всех шрифтов по умолчанию в любой момент:
plt.rcParams.update(plt.rcParamsDefault)
Вы можете найти больше руководств по Matplotlib здесь .
How to Change Font Sizes on a Matplotlib Plot
Often you may want to change the font sizes of various elements on a Matplotlib plot. Fortunately this is easy to do using the following code:
import matplotlib.pyplot as plt plt.rc('font', size=10) #controls default text size plt.rc('axes', titlesize=10) #fontsize of the title plt.rc('axes', labelsize=10) #fontsize of the x and y labels plt.rc('xtick', labelsize=10) #fontsize of the x tick labels plt.rc('ytick', labelsize=10) #fontsize of the y tick labels plt.rc('legend', fontsize=10) #fontsize of the legend
The following examples illustrates how to change the font sizes of various elements in the following matplotlib scatterplot:
import matplotlib.pyplot as plt x = [3, 4, 6, 7, 8] y = [12, 14, 15, 19, 24] plt.scatter(x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Note: The default font size for all elements is 10.
Example 1: Change the Font Size of All Elements
The following code shows how to change the font size of every element in the plot:
#set font of all elements to size 15 plt.rc('font', size=15) #create plot plt.scatter(x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Example 2: Change the Font Size of the Title
The following code shows how to change the font size of the title of the plot:
#set title font to size 50 plt.rc('axes', titlesize=50) #create plot plt.scatter(x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Example 3: Change the Font Size of the Axes Labels
The following code shows how to change the font size of the axes labels of the plot:
#set axes labels font to size 20 plt.rc('axes', labelsize=20) #create plot plt.scatter(x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Example 4: Change the Font Size of the Tick Labels
The following code shows how to change the font size of the tick labels of the plot:
#set tick labels font to size 20 plt.rc('xtick', labelsize=20) plt.rc('ytick', labelsize=20) #create plot plt.scatter(x, y) plt.title('title') plt.xlabel('x_label') plt.ylabel('y_label') plt.show()
Bonus: Restore the Default Font Sizes
You can use the following code to restore all fonts to their default size at any point:
plt.rcParams.update(plt.rcParamsDefault)
You can find more Matplotlib tutorials here.