[box type=”note” align=”” class=”” width=””]This is an excerpt from the book titled Matplotlib 2.x By Example written by Allen Chi Shing Yu, Claire Yik Lok Chung, and Aldrin Kay Yuen Yim,. The book covers basic know-how on how to create and customize plots by Matplotlib. It will help you learn to visualize geographical data on maps and implement interactive charts. [/box]
The article talks about how you can manipulate ticks in Matplotlib 2.0. It includes steps to adjust tick spacing, customizing tick formats, trying out the ticker locator and formatter, and rotating tick labels.
What are Ticks
Ticks are dividers on an axis that help readers locate the coordinates. Tick labels allow estimation of values or, sometimes, labeling of a data series as in bar charts and box plots.
Adjusting tick spacing
Tick spacing can be adjusted by calling the locator methods:
ax.xaxis.set_major_locator(xmajorLocator) ax.xaxis.set_minor_locator(xminorLocator) ax.yaxis.set_major_locator(ymajorLocator) ax.yaxis.set_minor_locator(yminorLocator)
Here, ax refers to axes in a Matplotlib figure.
Since set_major_locator()
or set_minor_locator()
cannot be called from the pyplot interface but requires an axis, we call pyplot.gca()
to get the current axes. We can also store a figure and axes as variables at initiation, which is especially useful when we want multiple axes.
Removing ticks
NullLocator
: No ticks
Drawing ticks in multiples
Spacing ticks in multiples of a given number is the most intuitive way. This can be done by using MultipleLocator
space ticks in multiples of a given value.
Automatic tick settings
MaxNLocator
: This finds the maximum number of ticks that will display nicely
AutoLocator
: MaxNLocator with simple defaults
AutoMinorLocator
: Adds minor ticks uniformly when the axis is linear
Setting ticks by the number of data points
IndexLocator
: Sets ticks by index (x = range(len(y))
Set scaling of ticks by mathematical functions
-
LinearLocator
: Linear scaleLogLocator
: Log scaleSymmetricalLogLocator
: Symmetrical log scale, log with a range of linearityLogitLocator
: Logit scaling
Locating ticks by datetime
There is a series of locators dedicated to displaying date and time:
MinuteLocator: Locate minutes HourLocator: Locate hours
DayLocator: Locate days of the month
WeekdayLocator: Locate days of the week
MonthLocator: Locate months, for example, 8 for August YearLocator: Locate years that in multiples
RRuleLocator: Locate using matplotlib.dates.rrulewrapper
The rrulewrapper is a simple wrapper around a
dateutil.rrule (dateutil) that allows almost arbitrary date
tick specifications
AutoDateLocator: On autoscale, this class picks the best
MultipleDateLocator to set the view limits and the tick locations
Customizing tick formats
Tick formatters control the style of tick labels. They can be called to set the major and minor tick formats on the x and y axes as follows:
ax.xaxis.set_major_formatter( xmajorFormatter ) ax.xaxis.set_minor_formatter( xminorFormatter ) ax.yaxis.set_major_formatter( ymajorFormatter ) ax.yaxis.set_minor_formatter( yminorFormatter )
Removing tick labels
NullFormatter
: No tick labels
Fixing labels
FixedFormatter
: Labels are set manually
Setting labels with strings
IndexFormatter
: Take labels from a list of strings
StrMethodFormatter
: Use the string format method
Setting labels with user-defined functions
FuncFormatter
: Labels are set by a user-defined function
Formatting axes by numerical values
ScalarFormatter
: The format string is automatically selected for scalars by default
The following formatters set values for log axes:
LogFormatter
: Basic log axis
LogFormatterExponent
: Log axis using exponent = log_base(value)
LogFormatterMathtext
: Log axis using exponent = log_base(value)
using Math text
LogFormatterSciNotation
: Log axis with scientific notation
LogitFormatter
: Probability formatter
Trying out the ticker locator and formatter
To demonstrate the ticker locator and formatter, here we use Netflix subscriber data as an example. Business performance is often measured seasonally. Television shows are even more “seasonal”. Can we better show it in the timeline?
import numpy as np
import matplotlib.pyplot as plt import matplotlib.ticker as ticker
"""
Number for Netflix streaming subscribers from 2012-2017
Data were obtained from Statista on https://www.statista.com/statistics/250934/quarterly-number-of-netflix-stre aming-subscribers-worldwide/ on May 10, 2017. The data were originally published by Netflix in April 2017.
"""
# Prepare the data set x = range(2011,2018)
y = [26.48,27.56,29.41,33.27,36.32,37.55,40.28,44.35,
48.36,50.05,53.06,57.39,62.27,65.55,69.17,74.76,81.5,
83.18,86.74,93.8,98.75] # quarterly subscriber count in millions
# Plot lines with different line styles plt.plot(y,'^',label = 'Netflix subscribers',ls='-')
# get current axes and store it to ax ax = plt.gca()
# set ticks in multiples for both labels ax.xaxis.set_major_locator(ticker.MultipleLocator(4)) # set major marks
# every 4 quarters, ie once a year ax.xaxis.set_minor_locator(ticker.MultipleLocator(1)) # set minor marks
# for each quarter ax.yaxis.set_major_locator(ticker.MultipleLocator(10)) # ax.yaxis.set_minor_locator(ticker.MultipleLocator(2))
# label the start of each year by FixedFormatter ax.get_xaxis().set_major_formatter(ticker.FixedFormatter(x))
plt.legend() plt.show()
From this plot, we see that Netflix has a pretty linear growth of subscribers from the year 2012 to 2017. We can tell the seasonal growth better after formatting the x axis in a quarterly manner. In 2016, Netflix was doing better in the latter half of the year. Any TV shows you watched in each season?
Rotating tick labels
A figure can get too crowded or some tick labels may get skipped when we have too many tick labels or if the label strings are too long. We can solve this by rotating the ticks, for example, by pyplot.xticks(rotation=60)
:
import matplotlib.pyplot as plt import numpy as np
import matplotlib as mpl mpl.style.use('seaborn')
techs = ['Google Adsense','DoubleClick.Net','Facebook Custom
Audiences','Google Publisher Tag', 'App Nexus'] y_pos = np.arange(len(techs))
# Number of websites using the advertising technologies
# Data were quoted from builtwith.com on May 8th 2017 websites = [14409195,1821385,948344,176310,283766]
plt.bar(y_pos, websites, align='center', alpha=0.5)
# set x-axis tick rotation plt.xticks(y_pos, techs, rotation=25) plt.ylabel('Live site count')
plt.title('Online advertising technologies usage') plt.show()
Use pyplot.tight_layout()
to avoid image clipping. Using rotated labels can sometimes result in image clipping, as follows, if you save the figure by pyplot.savefig()
. You can call pyplot.tight_layout()
before pyplot.savefig()
to ensure a complete image output.
We saw how ticks can be adjusted, customized, rotated and formatted in Matplotlib 2.0 for easy readability, labelling and estimation of values.
To become well-versed with Matplotlib for your day to day work, check out this book Matplotlib 2.x By Example.