Adding text in multi-panel figures

This section describes how to align our text and put it on the figure object rather than the axis objects.

The following is the code for the two-axis figure, with a sine curve and a cosine curve seperated by spine:

# Figure text
gs = mpl.gridspec.GridSpec(1,2, wspace=0.0)
nums = np.arange(0,10,0.1)
plt.subplot(gs[0])
plt.plot(nums, np.sin(nums))
plt.gca().spines['right'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.subplot(gs[1])
plt.plot(nums, np.cos(nums))
# plt.gca().yaxis.set_visible(False)
# plt.gca().spines['left'].set_visible(False)
plt.gca().xaxis.set_major_locator(mpl.ticker.MaxNLocator(5, prune='lower'))
# plt.figtext(0.5,0.5, "trig functions", horizontalalignment='center')

To span the text in this multiple panel plot, as shown in the following output, we will have to following some steps:

  1. Start by disabling the spine in the middle, so here we have a two-axis figure with a sine curve and a cosine curve:

  1. If we want to annotate this figure, we will use the figtext method; figtext takes the figure coordinate system. To give an example of what that does, when we pass trig functions as the string, it takes the same three kinds of arguments—x, y, and then the string. In fact, it is outside of the axes limits, as shown here:
# Figure text
gs = mpl.gridspec.GridSpec(1,2, wspace=0.0)
nums = np.arange(0,10,0.1)
plt.subplot(gs[0])
plt.plot(nums, np.sin(nums))
plt.gca().spines['right'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.subplot(gs[1])
plt.plot(nums, np.cos(nums))
plt.gca().yaxis.set_visible(False)
plt.gca().spines['left'].set_visible(False)
plt.gca().xaxis.set_major_locator(mpl.ticker.MaxNLocator(5, prune='lower'))
plt.figtext(0,0, "trig functions")

We will get the following output:

  1. To place this right in the middle, we can pass (0.5, 0.5). To get it centered, we will use horizontalalignment='center':
# Figure text
gs = mpl.gridspec.GridSpec(1,2, wspace=0.0)
nums = np.arange(0,10,0.1)
plt.subplot(gs[0])
plt.plot(nums, np.sin(nums))
plt.gca().spines['right'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.subplot(gs[1])
plt.plot(nums, np.cos(nums))
plt.gca().yaxis.set_visible(False)
plt.gca().spines['left'].set_visible(False)
plt.gca().xaxis.set_major_locator(mpl.ticker.MaxNLocator(5, prune='lower'))
plt.figtext(0.5,0.5, "trig functions", horizontalalignment='center')

We get the texts anchored to the very middle of this multi-panel figure, as shown here:

So, for multi-panel plots, figtext is to be used, and for single-panel plots, we will use the text so that it's anchored to the figure coordinates rather than to the data coordinates.

Hence, in order to place the text in the center of the plot, we are not sure about the data axes; the figure text is the tool to be used to place the text at the center.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.235.215