Adding text to both axis and figure objects

Let's start by importing everything we need:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
# Set up figure size and DPI for screen demo
plt.rcParams['figure.figsize'] = (6,4)
plt.rcParams['figure.dpi'] = 150
  1. Next, bring up the standard sine curve that we have been using:
# Add some text
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))

We will get the following output:

  1. We will add some text, say, right in the middle of the two curves. We will use the text method, and since we are taking the data coordinates, the middle of this plot would be (5, 0), so the arguments here are the X and Y coordinates, and then the text that you want to display would be seen as follows:
# Add some text
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5,0, "sine curve")

The output can be seen as follows:

But the preceding output does not look centered. In fact, from the preceding output, it's putting the beginning of our text at (5, 0).

  1. To get this centered, we will use the horizontal alignment keyword argument (by default, it's set to left). When changing it to center, we will see it then looks a lot more centered:
# Horizontal alignment
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", horizontalalignment='center')

We get the output as follows:

  1. You can also choose 'right', as shown:
# Horizontal alignment
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", horizontalalignment='right')

The output can be shown as follows:

  1. You can also change the font size by passing the size keyword argument that can take a size in points. We will insert the size as 18 for the pointer:
# Font size
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", size=18)

We will get the following output:

  1. For a small size keyword, insert the following:
# Font size
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", size=8)

The output can be seen as follows:

  1. We can pass a large font size:
# Font size
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", size='large')

We will get the following output:

  1. For an extra-large font size, use the following:
# Font size
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", size='x-large')

The output can be seen as follows:

  1. For a double-extra-large font, use the following code:
# Font size
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", size='xx-large')

We will get the following output:

This is often nice if you want figures that you can change to the actual figure size without having to worry about going in and then tweaking all of the font sizes.

  1. There are also a number of different keyword arguments to tweak the appearance of your fonts, including the family keyword arguments. So, by passing monospace, we will get a monospace font, as shown here:
# Font family, weight & style
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", family='monospace')

The output for the preceding code can be seen as follows:

  1. There's also the serif font if you want a Roman style font:
# Font family, weight & style
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", family='serif')

We will get the following output:

  1. You also have the ability to take a look at the weight by passing bold and getting a bold style:
# Font family, weight & style
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", weight='bold')

We will get the output as follows:

  1. We also have the style font. For example, to get an italic font, we pass style=' italic':
# Font family, weight & style
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", style='italic')

The following output is as follows:

  1. Often, we want this curve to point by using two keyword arguments, withdash=True and dashlength=24. We will add a little dash, as shown here:
# Adding dash withdash & dashlength
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", withdash=True, dashlength=24)

We will get the following output:

  1. By changing the data coordinates to (6, 0), we get a nice little dash pointing at the curve:
# Adding dash withdash & dashlength
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(6, 0, "sine curve", withdash=True, dashlength=24)

We get the following output from the preceding code:

In further sections, we will take a look at a much more versatile way of adding little dashes and arrows.

You can also pass a keyword argument that will rotate your text, so if we have to rotate the text by 45 degrees, pass rotation = 45 and it will rotate on this axis, so depending on what your horizontal alignment is, it will rotate around the (5,0) point. This could mean that the text is rotated about the center or it could mean that the text is rotated about one of the edges, as shown here:

# Rotating text
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve", rotation=45)

We will get the following output:

To add multiple lines, for example, if we want the word sine on top of the word curve, we need to put a newline character in the string. Since that's escaped, we would want to put an r to allow the literal, as shown here:

# Multi line
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.text(5, 0, "sine curve")

The output is shown as follows:

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

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