Rotate Text in Matplotlib Plots

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab will demonstrate how to rotate text objects in Matplotlib relative to a line or object on a plot rather than the screen coordinate system. This technique can be helpful when you want to rotate text in relation to something specific on the plot.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48984{{"`Rotate Text in Matplotlib Plots`"}} matplotlib/line_plots -.-> lab-48984{{"`Rotate Text in Matplotlib Plots`"}} python/variables_data_types -.-> lab-48984{{"`Rotate Text in Matplotlib Plots`"}} python/booleans -.-> lab-48984{{"`Rotate Text in Matplotlib Plots`"}} python/lists -.-> lab-48984{{"`Rotate Text in Matplotlib Plots`"}} python/tuples -.-> lab-48984{{"`Rotate Text in Matplotlib Plots`"}} python/data_collections -.-> lab-48984{{"`Rotate Text in Matplotlib Plots`"}} python/build_in_functions -.-> lab-48984{{"`Rotate Text in Matplotlib Plots`"}} end

Plot a diagonal line

First, we will plot a diagonal line at a 45-degree angle using Matplotlib's plot() function.

fig, ax = plt.subplots()

## Plot diagonal line (45 degrees)
h = ax.plot(range(0, 10), range(0, 10))

Adjust the limits of the plot

Next, we will adjust the limits of the plot so that the diagonal line is no longer at a 45-degree angle when viewed on the screen. This will create a scenario where we need to rotate text relative to the line, rather than the screen coordinate system.

## set limits so that it no longer looks on screen to be 45 degrees
ax.set_xlim([-10, 20])

Define text locations and rotation angle

We will now define the locations where we want to plot text on the figure and the angle of rotation we want to use.

## Locations to plot text
l1 = np.array((1, 1))
l2 = np.array((5, 5))

## Rotate angle
angle = 45

Plot text without correct rotation

We will now plot text at the specified locations without taking the rotation of the line into account. This will result in the text being rotated at a 45-degree angle, which is not what we want.

## Plot text
th1 = ax.text(*l1, 'text not rotated correctly', fontsize=16,
              rotation=angle, rotation_mode='anchor')

Plot text with correct rotation

Finally, we will plot text at the specified locations while taking the rotation of the line into account. This will result in the text being rotated at the correct angle relative to the line.

## Plot text
th2 = ax.text(*l2, 'text rotated correctly', fontsize=16,
              rotation=angle, rotation_mode='anchor',
              transform_rotates_text=True)

Display the figure

We will display the figure to see the difference between the two sets of plotted text.

plt.show()

Summary

In this lab, we learned how to rotate text objects in Matplotlib relative to a line or object on a plot. By using the transform_rotates_text parameter, we were able to ensure that the text was rotated at the correct angle in relation to the line, rather than the screen coordinate system.

Other Python Tutorials you may like