Introduction
In this tutorial, we will discuss how to change the font size of axis tick labels on a matplotlib figure in python. This is an important skill for visualizing data using matplotlib, as it allows us to customize our figures and make them more visually appealing.
I have personally utilized this technique when plotting time series data for example, and it has proven to be useful (see
How to plot a time series in python ? ).
Getting Started
Before we start changing the font size of axis tick labels, let's first create a simple matplotlib figure that we can work with:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x_values = [1.,2.,3.,4.,5.]
y_values = [1,4,9,16,25]
plt.scatter(x_values,y_values)
plt.title('How to change the font size \n of axis tick labels on a matplotlib figure ?', fontsize=14)
plt.savefig('matplotlib_font_size_axis_tick_labels_01.png', dpi=100, bbox_inches='tight')
plt.show()
The above code will create a simple line plot with 5 data points.
Now let's see how we can change the font size of axis tick labels on this figure.
Changing Font Size
To change the font size of axis tick labels, we will use the plt.xticks
and plt.yticks
functions. These functions allow us to specify the positions and labels for the ticks on our axes.
Changing font size for x-axis tick labels
By specifying a fontsize
parameter, we can change the font size of all the tick labels on our figure. You can also specify different font sizes for the x and y axes, or even for individual ticks.
plt.xticks(fontsize=14)
Changing font size for y-axis tick labels
plt.yticks(fontsize=14)
Changing font size for both axes
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
Customizing Tick Labels
In addition to changing the font size, we can also customize other aspects of our axis tick labels using the plt.xticks and plt.yticks functions. For example, we can change the color, rotation angle, and formatting of our tick labels.
For example, to change the color of x-axis tick labels to red, you can use the plt.xticks(color='red') command. If you want to rotate the x-axis tick labels by 45 degrees, use plt.xticks(rotation=45). For formatting the y-axis tick labels as percentages with two decimal places, you can use plt.yticks(format='%.2f%%'). Remember that there are various other parameters available to customize the tick labels, so make sure to refer to the documentation for more details. Additionally, you can utilize the plt.xlabel and plt.ylabel functions to add labels to the x and y axes respectively.
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(16,4))
x_values = [1.,2.,3.,4.,5.]
x_tick_positions = [1.,2.,3.,4.,5.]
x_tick_labels = ['2019-08-01','2019-08-02','2019-08-03','2019-08-04','2019-08-05']
y_values = [2,1,2,1,2]
plt.plot(x_values,y_values)
plt.xlabel('Days', fontsize=14, labelpad=20)
plt.ylabel('Values', fontsize=14, labelpad=20)
plt.title('How to plot a time series in python ?', fontsize=14)
plt.xticks(x_tick_positions, x_tick_labels, rotation=45, fontsize=14, color='red')
plt.yticks(fontsize=14)
ax.tick_params(axis='x', which='major', pad=15)
plt.savefig('time_series_04.png', dpi=100, bbox_inches='tight')
plt.show()
Output:
Utilizing the tick_params() function
Matplotlib also provides us with a built-in function called tick_params() that allows us to customize various properties of axis tick labels, including font size. To change the font size of axis tick labels, we need to specify labelsize parameter within tick_params() and assign it the desired font size value. Let's take a look at an example:
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import math
pi = math.pi
x_list = np.arange(-2*pi,2*pi,0.1)
y_list = [math.cos(x) for x in x_list]
fig = plt.figure(1)
plot = fig.add_subplot(111)
plt.plot(x_list,y_list)
plot.tick_params(axis='x', labelsize=14)
plt.grid()
plt.title('Change label axis font size in matplotlib')
plt.show()
References
Links | Site |
---|---|
matplotlib.pyplot.xticks | matplotlib.org |
matplotlib.pyplot.yticks | matplotlib.org |
How to modify the interval of the axis ticks in matplotlib ? | moonbooks.org |
How to plot a time series in python ? | moonbooks.org |