In this recipe, we will continue with setting axis and line properties and adding more data to our figure and charts.
Let's learn a bit about figures and subplots.
In matplotlib, figure()
is used to explicitly create a figure, which represents a user interface window. Figures are created implicitly just by calling plot()
or similar functions. This is fine for simple charts, but having the ability to explicitly create a figure and get a reference to its instance is very useful for more advanced use.
A figure contains one or more subplots. Subplots allow us to arrange plots in a regular grid. We already used subplot()
, in which we specify the number of rows and columns and the number of the plot we are referring to.
If we want more control, we need to use axes instances from the matplotlib.axes.Axes
class. They allow us to place plots at any location in the figure. An example of this would be to put a smaller plot inside a bigger one.
Ticks are part of figures. They consist of tick locators where ticks appear and tick formatters which show how ticks appear. There are major and minor ticks. Minor ticks are not visible by default. More importantly, major and minor ticks can be formatted and located independently of each other.
We can use matplotlib.pyplot.locator_params()
to control the behavior of tick locators. Even though tick locations are usually determined automatically, we can control the number of ticks and use a tight view if we want to when plots are smaller:
from pylab import * # get current axis ax = gca() # set view to tight, and maximum number of tick intervals to 10 ax.locator_params(tight=True, nbins = 10) # generate 100 normal distribution values ax.plot(np.random.normal(10, .1, 100)) show()
This should give us the following graph:
We see how the x and y axes are divided and what values are shown. We could have achieved the same setup using locator classes. Here we are saying "set the major locator to be a multiple of 10":
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(10))
Tick formatters can similarly be specified. Formatters specify how the values (usually numbers) are displayed. For example, matplotlib.ticker.FormatStrFormatter
simply specifies '%2.1f'
or '%1.1f cm'
as the string to be used as the label for the ticker.
Let's take a look at one example using dates.
Then we can use helper functions such as matplotlib.dates.date2num()
, matplotlib.dates.num2date()
, and matplotlib.dates.drange()
to convert dates between different representations.
Let's see another example:
from pylab import * import matplotlib as mpl import datetime fig = figure() # get current axis ax = gca() # set some daterange start = datetime.datetime(2013, 01, 01) stop = datetime.datetime(2013, 12, 31) delta = datetime.timedelta(days = 1) # convert dates for matplotlib dates = mpl.dates.drange(start, stop, delta) # generate some random values values = np.random.rand(len(dates)) ax = gca() # create plot with dates ax.plot_date(dates, values, linestyle='-', marker='') # specify formater date_format = mpl.dates.DateFormatter('%Y-%m-%d') # apply formater ax.xaxis.set_major_formatter(date_format) # autoformat date labels # rotates labels by 30 degrees by default # use rotate param to specify different rotation degree # use bottom param to give more room to date labels fig.autofmt_xdate() show()
3.148.108.112