Formatting Axes in Python-Matplotlib


Matplotlib is a popular data visualization library in Python that provides a variety of plots and charts to represent data in a meaningful way. One of the important aspects of data visualization is formatting the axes to efficiently communicate the information. In this blog, we will discuss different methods of formatting axes in Matplotlib and the use of Python code.

Matplotlib Axes

They are the regions of a Matplotlib figure where data can be plotted. A figure can have multiple axes that can be arranged in a grid-like pattern. Each axis can have a label, tick marks, tick labels, and other visual elements that can be customized to suit the visualization needs.

Formatting Axes

Matplotlib provides many options to customize the appearance of axes, such as changing the axis limits, adding grid lines, changing the tick marks and labels, and rotating the tick labels. We will discuss these options one by one.

  • Changing Axis Limits

  • Adding Grid Lines

  • Changing Tick Marks and Labels

Syntax

for x-axis
Axes.set_xlabel(self, xlabel, fontdict=None, labelpad=None, \*\*kwargs)
for y-axis
Axes.set_ylabel(self, ylabel, fontdict=None, labelpad=None, \*\*kwargs)

Example 1

Here's an example code for Labeling x, y-Axis in Python's Matplotlib library.

Algorithm

  • Import the necessary libraries: matplotlib.pyplot and numpy.

  • Create a sample data set using numpy.

  • Create a figure and axes using the subplots() method.

  • Set the x and y labels using set_xlabel() and set_ylabel() methods respectively.

  • Set the x and y limits using set_xlim() and set_ylim() methods respectively.

  • Set the x and y ticks using set_xticks() and set_yticks() methods respectively.

  • Set the tick labels using set_xticklabels() and set_yticklabels() methods respectively.

  • Set the title using the set_title() method.

  • Set the grid lines using grid() method.

  • Save the figure using savefig() method.

# importing matplotlib module
import matplotlib.pyplot as plt
import numpy as np

# x-axis & y-axis values
x = [1, 2, 3, 4, 5]
y = [10, 5, 15, 20, 25]

# create a figure and axes
fig, ax = plt.subplots()

# setting title to graph
ax.set_title('Tutorials Point')

# label x-axis and y-axis
ax.set_ylabel('y-AXIS')
ax.set_xlabel('x-AXIS')

# function to plot and show graph
ax.plot(x, y)
plt.show()

Output

Example 2

Formatting Axes in Python-matplotlib- Limits of x, y-axis which in this case is, (10,0) and (0,40) respectively. Limits of axes set the highest plots to be covered in the graph. By default the max value of x-axis and max value of y-axis of the given points will be pointed.

Algorithm

  • Import the necessary libraries, including Matplotlib.

  • Create a figure object using the plt.figure() function.

  • Create a set of axes using the plt.axes() function.

  • Use the plt.plot() feature to create your plot.

  • To label the axes, use the plt.xlabel() and plt.ylabel() functions, giving the preferred label as a string.

  • to provide the plot with a title, use the plt.identify() function.

  • Utilise functions like plt.xticks(), plt.yticks(), plt.grid(), and plt.tick_params() to adjust the tick labels and grid lines as necessary.

  • Utilise the plt.savefig() function to save the plot.

  • Use the plt.show() function to display the plot.

import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4, 5]
y = [10, 5, 15, 20, 25]

# create a figure and axes
fig, ax = plt.subplots()

ax.set_title('Tutorials Point')

ax.set_ylabel('y-AXIS')
ax.set_xlabel('x-AXIS')

# sets x, y-axis limits on the graph
ax.set_xlim(0, 10)
ax.set_ylim(0, 40)

# function to plot and show graph
ax.plot(x, y)
plt.show()

Output

Conclusion

formatting axes in Python's Matplotlib is an essential skill that every data analyst or scientist should possess. By tweaking the axes' attributes such as labels, ticks, limits, scales, and grids, we can create clear and informative visualizations that convey our insights effectively.

Updated on: 23-Aug-2023

588 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements