How to rotate text in Matplotlib – Python
Last Updated :
28 Apr, 2025
In this article, we will learn how to rotate text in Matplotlib in Python. Out of the many parameters in matplotlib.text(), there are three main parameters that will define text rotation and how the text will be rotated.
Syntax of matplotlib.text method
Syntax: matplotlib.text(x=0, y=0, text='', rotation=None, rotation_mode=None, transform_rotates_text=False)
Parameter:
- X, Y: x and y coordinate value
- text: str,Text value
- rotation: float or {'vertical', 'horizontal'}, Defines the angle in degrees between 0-360 for the text to rotate. '+ve' values for rotating the text anticlockwise and '-ve' values rotates the text clockwise. 'horizontal' = 0, 'vertical' = 90.
- rotation_mode: {None, 'default', 'anchor'}, Specifies the text rotation mode. None or "default": text is rotated first, then aligned performed.
- anchor: rotation of the text occurs after alignment.
- transform_rotates_text: bool
Return: whether rotations of the transform affect the text direction.
Stepwise Implementation
We can now add these parameters and change their values as we want to rotate the text in the plot.
Step 1: Import the library
Python3
import matplotlib.pyplot as plt
Step 2: Set the x and y-axis range.
Python3
plt.xlim(0, 3)
plt.ylim(0, 5)
Step 3: Plotting a line for reference.
Python3
plt.plot((1, 4), (1, 1), 'b')
Step 4: Insert text normally with color.
Python3
plt.text(1, 1, '(1) Geeks_for_Geeks', color='green')
Step 5: Insert text rotated default.
Python3
plt.text(1, 2, '(3) Geeks_for_Geeks 30°',
color = 'green', rotation = 30,
rotation_mode = 'anchor')
Step 6: Insert text rotated relatively to plot coordinates.
Python3
plt.text(1, 1.5, '(2) Geeks_for_Geeks 30° relative',
color = 'green', rotation = 30,
rotation_mode = 'anchor',
transform_rotates_text = True)
Complete Code:
In the figure, we can see that, (1) is a normal text placed horizontally in the plot, (2) is a text rotated 30o with respect to plot coordinates, and (3) is a text rotated 30o by default.
Python3
# Text rotation relative to line in matplotlib
import matplotlib.pyplot as plt
# setting x and y axis range
plt.xlim(0, 3)
plt.ylim(0, 5)
# plotting a line for reference
# plt.plot((1, 4), (1, 1), 'b')
# inserting text normally
plt.text(1, 1, '(1) Geeks_for_Geeks', color='green')
# Inserting text rotated default
plt.text(1, 2, '(3) Geeks_for_Geeks 30°', color='green',
rotation=30, rotation_mode='anchor')
# Inserting text rotated relatively to plot coordinates
plt.text(1, 1.5, '(2) Geeks_for_Geeks 30° relative',
color='green',
rotation=30, rotation_mode='anchor',
transform_rotates_text=True)
# Display plot
plt.show()
Output:
Similar Reads
How to Rotate X-Axis Tick Label Text in Matplotlib? Rotating X-axis tick labels in Matplotlib improves readability, especially when labels are long or overlap. Angles like 45° or 90° help display dates or categories clearly. Matplotlib provides several easy ways to rotate labels at both the figure and axes levels. Letâs explore the most effective one
3 min read
How to Adjust Title Position in Matplotlib? In this article, you learn how to modify the Title position in matplotlib in Python. Method 1: Using matplotlib.pyplot.title() function The title() method in matplotlib module is used to specify title of the visualization depicted and displays the title using various attributes. Syntax: matplotlib.p
3 min read
How to Add Title to Subplots in Matplotlib? In this article, we will see how to add a title to subplots in Matplotlib? Let's discuss some concepts : Matplotlib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work
3 min read
Matplotlib.axes.Axes.text() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read
How to Set X-Axis Values in Matplotlib in Python? In this article, we will be looking at the approach to set x-axis values in matplotlib in a python programming language. The xticks() function in pyplot module of the Matplotlib library is used to set x-axis values. Syntax: matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs) xticks() functio
2 min read
Matplotlib.pyplot.text() function in Python This function is used to add a text to the axes at location x, y in data coordinates. Syntax: matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)parameters                                               Descriptionx, y:floatThe position to place the tex
1 min read