A Guide to Tick Customizations in MatPlotLib: Topics in MatPlotLib

Introduction to Tick Customizations in MatPlotLib

The MatPlotLib series has explored a wide variety of topics in the MatPlotLib library. In one regard, we’ve spent a great deal of time elaborating on plot types. In particular, we have discussed in great depths the plotting of simple line plotsscatter plotsplotting density and contour plots, as well as a variety of histogram types. Furthermore, we have devoted significant attention towards customization features with plots. In this capacity, we focused explored on customizing plot legendscustomizing plot colorbars, and including figure annotations in MatPlotLib. When we think of data science, we often think about the importance of data modeling, but the customizations available are essential for constructing coherent models. We continue on with this subject by addressing alterations to figure tick customizations in MatPlotLib.

Tick Customizations in MatPlotLib

With almost any plot we may create in MatPlotLib, chart axes are typically intercalated by tick marks serving to denote continuous values along a spectrum. MatPlotLib has default settings for axis ticks when creating a plot, but in a myriad of instances, users may prefer more customized tick marks. Fortunately, MatPlotLib affords users with a plethora of tick locators and formats to facilitate user control over plot.

The axis we observe in a graph are themselves an instance of the chart object. Thus, the tick marks we observe associate with the graph are sub-instances of the axis object. This elucidation falls in line with our understanding of a MatPlotLib plot as a bundle of objects associated with the graph.

Firstly, both the ‘x’ and ‘y’ axes possess their own tick attributes, thus we may specify them individually. Furthermore, these tick attributes can be differentiated according to major and minor ticks. These major tick marks are more prominent along the axis, while the minor ticks are less obvious. Both of these tick types may be manipulated by the user to improve interpretation of graphical data.

Let us begin our analysis by exploring the various tick functions on a chart with empty data. If we seek to manually specify the axis tick marks, when plotting a figure according to its aces, we can use the ‘xscale’ and ‘yscale’ attributes to control the tick marks. Let’s code an empty plot utilizing various tick mark systems.

We begin here encoding an empty plot by invoking the seaborn whitegrid style sheet, and setting the axis labels to log. Check the code out first:

When we execute this code, an empty plot with the following code reveals:

We may note that in the plot above, there are a series of evident tick marks associated with a specific logarithmic value.

Fortunately, MatPlotLib offers a variety of axis scales for tick marks beyond just logarithms, including linear scales, symlog scales, and logit scales. Let’s check some of these out:

Linear Scale
Symlog Scale

Elaborating Tick Attributes

On a given axis, the major tick marks exhibit a large tick associated with a quantitative label for the tick. The minor ticks do not possess associated labels and are also much smaller. We can elucidate the reason for these differential visibility attributes by observing the ‘formatter’ characteristic associated with either axis. We can discern the formatter characteristic of both the major and minor ticks by using the ‘get_major_formatter’ function. Check out the code for doing so here:

When we run this code to discern the formatting feature of the ‘x’ axis’s major and minor tick marks, the output returns formatting attributes of the form:

In accessing the formatting attributes of the linear scale, we see that the major tick marks possess a scalar formatter object while the minor tick marks have a null formatter object. The ‘NullFormatter’ attribute is a significant feature to note as this reveals that these tick marks will not be associated with a label.

Altering Tick Visibility

Taking stock of the ‘NullFormatter’ and ‘NullLocator’, we can employ these attributes ourselves to control the visibility of tick marks and tick labels on either of the axes. For example, with our previous plot, we can hide the logarithmic tick marks and tick labels using the set_major_locator and set_major_formatter functions. Take a look at the following code which demonstrates how to hide the tick marks and labels:

Now, the plot that results from this code appears as:

Note that when setting the tick attributes to null for the x-axis, not only have we removed the major tick marks but also the labels associated with those marks.

Specifying Tick Mark Spacing

When we run our plot with linear scales on its axes, we obtain a plot that looks as follows:

We can see on the x-axis that major tick marks are plotted every .2 units. However, what if we wanted the x-axis to be delimited on a scale of, say, every .1 units. This action might be motivated to facilitate better interpretation of smaller values. Executing this function may occur by the use of plt.MaxNLocator function. Let’s observe how we may do this in our code:

When we set this MaxNLocator value to 11, the plot makes major tick marks on the x-axis every .1 units. It procures a plot of the following form:

Sophisticated Tick Marks

Numerical are not always the type of tick marks we need when plotting graphical data. For example, when plotting trigonometric functions, it is often preferable to set the axis tick marks to multiples of pi. Let’s first take a look at the code for plotting this function:

Here, we create a set of input values and plot these values with the sine and cosine functions. This creates a plot of the form:

However, suppose we desire to set the x-axis ticks as relative to multiples of pi. We may specify this by using the MultipleLocator function, which with our code appears as:

With this code, the use of the plt.MultipleLocator function establishes tick marks at every one-half multiple of pi. Utilizing this code produces a plot of the following form:

The Take Away

When using MatPlotLib to create quantitative plots, there are a myriad of circumstances wherein providing a different type of grid system is a desired outcome. For example, with alternative functions, we may prefer a logarithmic scale over a linear scale, or vice versa. Additionally, if we seek to improve precision of a graph, we can increase the number of tick labels on an axis (or decrease them, if desired). Furthermore, we can hide these tick marks altogether. Ultimately, there is a wide variety of means of tick customizations in MatPlotLib, as well as tick labels. If you seek to check this subject out further, consider reading the MatPlotLib online manual, which may be found here. Nevertheless, in our subsequent article in this series, we extrapolate on more computational topics. Specifically, we explore three dimensional plots in MatPlotLib. Enjoy!

Leave a Reply

%d bloggers like this: