Explain the following with respect to Matplotlib1) Labels Titles Text Annotations Legends 2) Subplots

10 a] Explain the following with respect to Matplotlib 1) Labels Titles Text Annotations Legends 2) Subplots

Labels

Matplotlib provides a few label functions that we can use for setting labels to the x- and y-axes. The plt.xlabel() and plt.ylabel() functions are used to set the label for the current axes. The set_xlabel() and set_ylabel() functions are used to set the label for specified axes.
Example:

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
Titles

A title describes a particular chart/graph. The titles are placed above the axes in the center, left edge, or right edge. There are two options for titles – you can either set the Figure title or the title of an Axes. The suptitle() function sets the title for the current and specified Figure. The title() function helps in setting the title for the current and specified axes.
Example:

fig = plt.figure()
fig.suptitle('Suptitle', fontsize=10, fontweight='bold')

This creates a bold Figure title with a text subtitle and a font size of 10:

plt.title('Title', fontsize=16)

The plt.title function will add a title to the Figure with text as Title and font size of 16 in this case.

Text

There are two options for text – you can either add text to a Figure or text to an Axes. The figtext(x, y, text) and text(x, y, text) functions add text at locations x or y for a Figure.
Example:

ax.text(4, 6, 'Text in Data Coords', bbox={'facecolor': 'yellow', 
'alpha':0.5, 'pad':10})
Annotations

Compared to text that is placed at an arbitrary position on the Axes, annotations are used to annotate some features of the plot. In annotations, there are two locations to consider: the annotated location, xy, and the location of the annotation, text xytext. It is useful to specify the parameter arrowprops, which results in an arrow pointing to the annotated location.
Example:

ax.annotate('Example of Annotate', xy=(4,2), xytext=(8,4), 
arrowprops=dict(facecolor='green', shrink=0.05))
Legends

Legend describes the content of the plot. To add a legend to your Axes, we have to specify the label parameter at the time of plot creation. Calling plt.legend() for the current Axes or Axes.legend() for a specific Axes will add the legend. The loc parameter specifies the location of the legend.

Example

plt.plot([1, 2, 3], label='Label 1')
plt.plot([2, 4, 3], label='Label 2')
plt.legend()

ii] Subplots

It is often useful to display several plots next to one another. Matplotlib offers the concept of subplots, which are multiple Axes within a Figure. These plots can be grids of plots, nested plots, and so on.
Explore the following options to create subplots:

  • The plt.subplots(, ncols) function creates a Figure and a set of subplots. nrows, ncols define the number of rows and columns of the subplots, respectively.
  • The plt.subplot(nrows, ncols, index) function or, equivalently, plt. subplot(pos) adds a subplot to the current Figure. The index starts at 1. The plt.subplot(2, 2, 1) function is equivalent to plt.subplot(221).
  • The Figure.subplots(nrows, ncols) function adds a set of subplots to the specified Figure
  • The Figure.add_subplot(nrows, ncols, index) function or, equivalently, Figure.add_subplot(pos), adds a subplot to the specified Figure.

plt.subplot and Figure.add_subplot have the option to set a projection. For a polar projection, either set the projection='polar' parameter or the parameter polar=True parameter.

Example:

fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
axes = axes.ravel()
for i, ax in enumerate(axes):
    ax.plot(series[i])

Setting sharex and sharey to True results in the following diagram. This allows for a better comparison:

subplots with a shared x and y axis

Leave a Reply

Your email address will not be published. Required fields are marked *