Consider the following code that places text on a plot using ax.text. What text will appear at coordinates (0.5, 0.5)?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.text(0.5, 0.5, 'Center', ha='center', va='center') plt.close(fig) print(ax.texts[0].get_text())
Look at the third argument of ax.text. It is the string that will be displayed.
The ax.text function places the string given as the third argument at the specified coordinates. Here, the string is "Center".
After running the code below, how many text objects are present on the axes?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.text(0.1, 0.1, 'A') ax.text(0.2, 0.2, 'B') ax.text(0.3, 0.3, 'C') plt.close(fig) print(len(ax.texts))
Each call to ax.text adds one text object to the axes.
There are three calls to ax.text, so three text objects are added.
Given the code below, which option correctly describes the vertical alignment of the text?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.text(0.5, 0.5, 'Text', va='top') plt.close(fig)
Vertical alignment va='top' means the top of the text is at the given y coordinate.
Setting va='top' places the top edge of the text at the y coordinate.
In matplotlib, what does setting ha='right' do to the text placement?
Horizontal alignment ha='right' aligns the right edge of the text to the x coordinate.
Setting ha='right' means the text's right edge is at the x coordinate.
What error will this code raise?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.text(0.5, 0.5, 'Hello', ha='middle') plt.close(fig)
Check the valid values for ha in matplotlib documentation.
The value 'middle' is not valid for ha. Valid values are 'left', 'center', 'right'.