Open In App

How to rotate text in Matplotlib – Python

Improve
Improve
Like Article
Like
Save
Share
Report

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:

How to rotate text in Matplotlib – Python

 



Last Updated : 12 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads