Normalized coordinates refers to plot coordinates where the horizontal and vertical axes run from 0.0 to 1.0 or zero percent to 100 percent of the plot dimensions. One often wants to position text labels or annotations in this way. This is NOT the default for the plt.text(…) function in matplotlib, the plotting add on module for Python that emulates MATLAB style plotting.
The key functions in matplotlib for normalized coordinates are:
ax = plt.gca() # get the current axes of the plot
plt.text(x, y, "my text", transform=ax.transAxes, fontsize)
This is a short program showing the full use of these two function in context.
# How to plot text in normalized coordinates (0, 1) in matplotlib
# ax = plt.gca() # get current plot axes object
# plt.text(x, y, string_message, transform = ax.transAxes, fontsize)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.0, 10.0, 100)
y = 10*x**2
fig = plt.figure()
plt.title("How to plot text in normalized coordinates in matplotlib", fontsize=8)
plt.plot(x, y, 'g-', label='parabola')
# Plot text in normalized (0.0, 1.0) coordinates in matplotlib
ax = plt.gca() # get current plot axes object
# NOTE: lowercase first letter of transAxes below
Y_POS = 0.91 # in range 0.0 to 1.0 (100%)
this_text = f"plt.text(0.01, {Y_POS}, \"this text\", transform=ax.transAxes"
plt.text(0.01, 0.95, "ax = plt.gca()", transform=ax.transAxes)
plt.text(0.01, Y_POS, this_text, transform=ax.transAxes)
#
plt.minorticks_on()
plt.grid(True, which='major', color='k')
plt.grid(True, which='minor')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
fig.savefig('matplotlib_normalized_coordinates.jpg', dpi=72)
(C) 2022 by John F. McGowan, Ph.D.
About Me
John F. McGowan, Ph.D. solves problems using mathematics and mathematical software, including developing gesture recognition for touch devices, video compression and speech recognition technologies. He has extensive experience developing software in C, C++, MATLAB, Python, Visual Basic and many other programming languages. He has been a Visiting Scholar at HP Labs developing computer vision algorithms and software for mobile devices. He has worked as a contractor at NASA Ames Research Center involved in the research and development of image and video processing algorithms and technology. He has published articles on the origin and evolution of life, the exploration of Mars (anticipating the discovery of methane on Mars), and cheap access to space. He has a Ph.D. in physics from the University of Illinois at Urbana-Champaign and a B.S. in physics from the California Institute of Technology (Caltech).