Formatting Axes in Python-Matplotlib
Matplotlib is a python library for creating static, animated and interactive data visualizations.
Note: For more information, refer to Introduction to Matplotlib
What is Axes?
This is what you think of as ‘plot’. It is the region of the image that contains the data space. The Axes contains two or three-axis(in case of 3D) objects which take care of the data limits. Below is an image illustrating the different parts of a figure which contains the graph.
The different aspects of the Axes can be changed according to the requirements.
1. Labelling x, y-Axis
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)
These functions are used to name the x-axis and y-axis.
Output:
2. Limits of x, y-Axis
- left and right – float, optional
The left xlim(starting point) and right xlim(ending point) in data coordinates. Passing None leaves the limit unchanged. - auto – bool or None, optional
To turn on autoscaling of the x-axis. True turns on, False turns off (default action), None leaves unchanged. - xmin, xmax : They are equivalent to left and right respectively, and it is an error to pass both xmin and left or xmax and right.
Returns:
right, left – (float, float)- bottom and top – float, optional
The bottom ylim(starting point) and top ylim(ending point) in data coordinates. Passing None leaves the limit unchanged. - auto – bool or None, optional
To turn on autoscaling of the y-axis. True turns on, False turns off (default action), None leaves unchanged. - ymin, ymax : They are equivalent to left and right respectively, and it is an error to pass both ymin and left or ymax and right.
Returns:
bottom, top – (float, float)Add a title and axis labels to your charts using matplotlib
In this post, you will see how to add a title and axis labels to your python charts using matplotlib. If you’re new to python and want to get the basics of matplotlib , this online course can be interesting.
In the following example, title, x label and y label are added to the barplot using the title() , xlabel() , and ylabel() functions of the matplotlib library.
Those functions are applied to a barplot in the example, but the same method would work for other chart types.
# libraries import numpy as np import matplotlib.pyplot as plt # create dataset height = [3, 12, 5, 18, 45] bars = ('A', 'B', 'C', 'D', 'E') x_pos = np.arange(len(bars)) # Create bars and choose color plt.bar(x_pos, height, color = (0.5,0.1,0.5,0.6)) # Add title and axis names plt.title('My title') plt.xlabel('categories') plt.ylabel('values') # Create names on the x axis plt.xticks(x_pos, bars) # Show graph plt.show()
Note: the matplotlib section provides a lot of tips and tricks on how to customize a matplotlib chart!
- bottom and top – float, optional